这里要讲根据电池的当前特征对任务排期。对一个比较耗电的任务,需要在电量比较丰盈的情况下完成,如果电量已经枯萎了,做这样的任务,则会耗尽所有电量。
基于此,在做这样的任务前,获取当前电池特征,并利用电池特征做出是否要执行任务的判断。
详细讲解如下:
1 获取当前的电池特征
/* 获取当前电池状态。将BroadcastReceiver 设为null 则会直接返回当前电池的状态。*/ IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = registerReceiver(null, ifilter); /** * 电池充电状态 * public static final int BATTERY_STATUS_UNKNOWN = 1; 未知状态 * public static final int BATTERY_STATUS_CHARGING = 2; 充电中 * public static final int BATTERY_STATUS_DISCHARGING = 3; 放电中 * public static final int BATTERY_STATUS_NOT_CHARGING = 4; 未纯电 * public static final int BATTERY_STATUS_FULL = 5; 电满状态 */ int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); /** * 电池充电方式, 如果手机正在充电情况下,则可以做一些耗电的操作,其中AC充电时可以最大限度的使用。其次USB充电 * public static final int BATTERY_PLUGGED_USB = 2; USB充电 * public static final int BATTERY_PLUGGED_WIRELESS = 4; 无线充电 * public static final int BATTERY_PLUGGED_AC = 1; AC充电,交流电充电 */ int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); /** * 电池电量水平 */ int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale; /** * public static final int BATTERY_HEALTH_UNKNOWN = 1; 健康状态未知 * public static final int BATTERY_HEALTH_GOOD = 2; 健康 * public static final int BATTERY_HEALTH_OVERHEAT = 3; 过热 * public static final int BATTERY_HEALTH_DEAD = 4; 没电 * public static final int BATTERY_HEALTH_OVER_VOLTAGE = 5; 过电压 * public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6; 未定义错误 * public static final int BATTERY_HEALTH_COLD = 7; 冷却 */ int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
至于应该在那个时机执行任务,我想读者能够分辨的出,只要别再BATTERY_HEALTH_DEAD 的时候安排任务即可。
2 监听电池充电状态。电池充电往往是干活的好时机,那时候随便你干,电量都能满足
注册监听
<receiver android:name=".BattertyConnectReceiver"> <intent-filter> <!-- 手机链接充电或断开充电引起变化 --> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> </intent-filter> </receiver>
接收广播
public class BattertyConnectReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); if(status == BatteryManager.BATTERY_STATUS_CHARGING){ //充电状态,好时机 }else if(status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){ //断开充电状态 } } }
3 监听电池低电变化状态
注册监听
<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>
接收广播
public class BatteryLevelReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(ACTION_BATTERY_LOW.equals(action)){ //电量走向不足档位,做任务状态保存 }else if(ACTION_BATTERY_OKAY.equals(action)){ //电量走向充足档位 } } }
以上API是正对电池特征的一些基本的判断。需要在恰当的电池状态做恰当任务
时间: 2024-10-13 01:38:27