1、电池优化
作为app开发者,或许很少有人会注意app对电量的损耗,但是用户对电量可是很敏感的,app做好电量损耗的优化会为自己的app加分不少。
如果是一个好的负责任的开发者,就应该限制app对电量的影响,当没有网络连接的时候,禁用后台服务更新,当电池电量低的时候减少更新的频率,确保自己的app对电池的影响降到最低。当电池充电或者电量比较饱和时,可以最大限度的发挥app的刷新率
<receiver android:name=".PowerConnectReceiver"> <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 PowerConnectionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; int chargeFlag = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargeFlag == BATTERY_PLUGGED_USB; boolean acCharge = chargeFlag == BATTERY_PLUGGED_AC; } }
//获取程序是否充电 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,-1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||status == BatteryManager.BATTERY_STATUS_FULL;
// 充电方式,usb还是电源 int chargeFlag = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargeFlag == BATTERY_PLUGGED_USB; boolean acCharge = chargeFlag == BATTERY_PLUGGED_AC;
不断的检测电量也会影响电池的使用时间,我们可以这样做
<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>
当电量低或者满时会触发
{}