文章来源:http://wuxiaolong.me/2017/04/27/AndroidBattery/
我们可以获取当前充电状态、监控充电状态变化、监控电池电量变化
获取当前充电状态
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
简单地调用 registerReceiver 传入 null 作为接收器来注册 BroadcastReceiver,便可返回当前电池状态 Intent。
// 充电状态
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
Log.d("wxl", "isCharging=" + isCharging);
Toast.makeText(this,"isCharging=" + isCharging,Toast.LENGTH_LONG).show();
// 设备是通过 USB 还是交流充电器进行充电
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
Log.d("wxl", "usbCharge=" + usbCharge + ",acCharge=" + acCharge);
Toast.makeText(this,"usbCharge=" + usbCharge + ",acCharge=" + acCharge,Toast.LENGTH_LONG).show();
监控充电状态变化
清单文件中注册一个 BroadcastReceiver,通过在一个 Intent 过滤器内定义 ACTION_POWER_CONNECTED 和 ACTION_POWER_DISCONNECTED 来同时侦听这两种事件
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
PowerConnectionReceiver
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
String batteryStatus = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
batteryStatus = "正在充电";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
batteryStatus = "正在放电";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
batteryStatus = "未充电";
break;
case BatteryManager.BATTERY_STATUS_FULL:
batteryStatus = "充满电";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
batteryStatus = "未知道状态";
break;
}
Toast.makeText(context, "batteryStatus=" + batteryStatus, Toast.LENGTH_LONG).show();
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC);
String chargePlug = "";
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
chargePlug = "AC充电";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
chargePlug = "USB充电";
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
chargePlug = "无线充电";
break;
}
Toast.makeText(context, "chargePlug=" + chargePlug, Toast.LENGTH_LONG).show();
}
}
注册
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(new PowerConnectionReceiver(), intentFilter);
监控电池电量变化
清单文件中注册一个 BroadcastReceiver,通过侦听 ACTION_BATTERY_LOW 和 ACTION_BATTERY_OKAY,每当设备电池电量不足或退出不足状态时,便会触发该接收器。
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
</intent-filter>
</receiver>
BatteryLevelReceiver
public class BatteryLevelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//当前剩余电量
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
//电量最大值
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
//电量百分比
float batteryPct = level / (float) scale;
Log.d("wxl", "batteryPct=" + batteryPct);
Toast.makeText(context, "batteryPct=" + batteryPct, Toast.LENGTH_LONG).show();
}
}
注册
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(new BatteryLevelReceiver(), intentFilter);
通常,如果设备连接了交流充电器,您应该最大限度提高后台更新的频率;而如果设备是通过 USB 充电,则应降低更新频率,如果电池正在放电,则应进一步降低更新频率;在电池电量极低时停用所有后台更新。
网友评论