代码如下
//需要添加权限 <uses-permission android:name="android.permission.BATTERY_STATS" />
function listen_Battery(){
try{
var main = plus.android.runtimeMainActivity(); //获取activity
var Intent = plus.android.importClass('android.content.Intent');
var receiver;
recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
try {
var BatteryManager = plus.android.importClass('android.os.BatteryManager');
var action = intent.getAction();
if (action == Intent.ACTION_BATTERY_CHANGED) {
var status = intent.getIntExtra("status", 0);
var health = intent.getIntExtra("health", 0);
var present = intent.getBooleanExtra("present", false);
var level = intent.getIntExtra("level", 0);
var scale = intent.getIntExtra("scale", 0);
var icon_small = intent.getIntExtra("icon-small", 0);
var plugged = intent.getIntExtra("plugged", 0);
var voltage = intent.getIntExtra("voltage", 0);
var temperature = intent.getIntExtra("temperature", 0);
var technology = intent.getStringExtra("technology");
var statusString = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
statusString = "unknown";
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "discharging";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "not charging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "full";
break;
}
var healthString = "";
switch (health) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
healthString = "unknown";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthString = "good";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthString = "overheat";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
healthString = "dead";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthString = "voltage";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthString = "unspecified failure";
break;
}
var acString = "";
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
acString = "plugged ac";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
acString = "plugged usb";
break;
}
var s="";
s = "status:"+statusString+"\n"
+"health:"+healthString+"\n"
+"present:"+present+"\n"
+"level:"+level+"\n"
+"scale:"+scale+"\n"
+"icon_small:"+ icon_small+"\n"
+"plugged:"+acString+"\n"
+"voltage:"+voltage+"\n"
+"temperature:"+temperature+"\n"
+"technology:"+technology+"\n";
alert(s);
main.unregisterReceiver(receiver);
}
} catch (e1) {
console.log(e1);
}
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
main.registerReceiver(receiver, filter); //注册监听
}catch(e){
console.log(e);
}
}
Android_磊子 (作者)
不能用?有什么提示吗?
2016-02-01 15:53
球风无痕
没有提示,我也添加了权限,用按钮调用了下,控制台和手机没有任何反映
2016-02-01 15:55
球风无痕
安卓手机 小米note测试的
2016-02-01 15:55
Android_磊子 (作者)
回复 球风无痕:目前还没有调通吗?
2016-03-07 10:29