1. 功能
打开/关闭手电筒、获取电池电量、获取手机系统。
2. 使用
该插件为本人撰写,点击pub.dev查看。
dependencies:
control_center: ^0.0.4
import 'package:control_center/control_center.dart';
final _controlCenterPlugin = ControlCenter();
//打开/关闭手电筒
bool switchBool = await _controlCenterPlugin.openOrCloseFlashlight();
//获取手机电池电量
int batteryLevel = await _controlCenterPlugin.getBatteryLevel()
//获取手机系统版本
String platformVersion = await _controlCenterPlugin.getPlatformVersion();
3. 代码
* iOS
public class SwiftControlCenterPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "control_center", binaryMessenger: registrar.messenger())
let instance = SwiftControlCenterPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "getPlatformVersion" {
result("iOS " + UIDevice.current.systemVersion)
} else if call.method == "getBatteryLevel" {
let device = UIDevice.current
device.isBatteryMonitoringEnabled = true
if device.batteryState == UIDevice.BatteryState.unknown {
result(FlutterError(code: "UNAVAILABLE",message: "Battery info unavailable",details: nil))
} else {
result(Int(device.batteryLevel * 100))//电池电量
}
}else if call.method == "openOrCloseFlashlight" {
// 创建Device实例对象
let device = AVCaptureDevice.default(for: AVMediaType.video)
if device!.hasTorch && ((device?.isTorchAvailable) != nil){
// 加锁
try? device?.lockForConfiguration()
if device?.torchMode == .off{
// 开灯
device?.torchMode = .on
result(true)
}else{
// 关灯
device!.torchMode = .off
result(false)
}
// 解锁
device!.unlockForConfiguration()
}
}
result(FlutterMethodNotImplemented)
}
}
* Android
private lateinit var channel : MethodChannel
private lateinit var myContext: Context
private lateinit var manager: CameraManager
private lateinit var mCamera: Camera
private var isFlashOn = false //记录手电筒状态
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "control_center")
channel.setMethodCallHandler(this)
myContext=flutterPluginBinding.applicationContext
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager = myContext.getSystemService(Context.CAMERA_SERVICE) as CameraManager
}
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
}else if (call.method == "getBatteryLevel") {
val batteryLevel = getBatteryLevel()
result.success(batteryLevel)
}else if (call.method == "openOrCloseFlashlight") {
val lightStatus = openOrCloseFlashlight()
result.success(lightStatus)
}else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
//获取电池电量
private fun getBatteryLevel(): Int {
val batteryLevel: Int
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val batteryManager = myContext.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
} else {
val intent = ContextWrapper(myContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
}
return batteryLevel
}
//打开闪光灯
private fun openOrCloseFlashlight(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.setTorchMode("0", !isFlashOn)
isFlashOn=!isFlashOn
}else{
var camera = Camera.open()
var parameters = camera.parameters
if (isFlashOn) {
parameters.flashMode = Camera.Parameters.FLASH_MODE_OFF
} else {
parameters.flashMode = Camera.Parameters.FLASH_MODE_TORCH
}
camera.parameters = parameters
isFlashOn = !isFlashOn
}
return isFlashOn
}
4. Demo
Github: control_center
网友评论