uni-app 获取手机的充电状态和电量
苹果手机获取手机电量和充电状态
var UIDevice = plus.ios.import('UIDevice') var dev = UIDevice.currentDevice() if (!dev.isBatteryMonitoringEnabled()) { dev.setBatteryMonitoringEnabled(true) } var level = dev.batteryLevel() // 获取苹果手机电量 电量要*100 var status = dev.batteryState() // 获取充电状态 状态2或者3 代表充电连接数据线
安卓手机获取手机电量和充电状态
var main = plus.android.runtimeMainActivity() var Intent = plus.android.importClass('android.content.Intent') var recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', { onReceive: function(context, intent) { console.log('intent', intent) var action = intent.getAction() if (action == Intent.ACTION_BATTERY_CHANGED) { var level = intent.getIntExtra('level', -1) // 电量 var status = intent.getIntExtra('status', -1) // 充电状态 状态为2代表充电 } } })