When you're altering the frequency of your background updates to reduce the effect of those updates on battery life, checking the current battery level and charging state is a good place to start.
The battery-life impact of performing application updates depends on the battery level and charging state of the device. The impact of performing updates while the device is charging over AC is negligible, so in most cases you can maximize your refresh rate
whenever the device is connected to a wall charger. Conversely, if the device is discharging, reducing your update rate helps prolong the battery life.
Similarly, you can check the battery charge level, potentially reducing the frequency of—or even stopping—your updates when the battery charge is nearly exhausted.
Determine the Current Charging State
Start by determining the current charge status. The BatteryManager
broadcasts all battery and charging details in a sticky
Intent
that includes the charging status.
Because it's a sticky intent, you don't need to register a BroadcastReceiver
—by simply calling
registerReceiver
passing in null
as the receiver as shown in the next snippet, the current battery status intent is returned. You could pass in an actual
BroadcastReceiver
object here, but we'll be handling updates in a later section so it's not necessary.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
You can extract both the current charging status and, if the device is being charged, whether it's charging via USB or AC charger:
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
// How are we charging?
int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
Typically you should maximize the rate of your background updates in the case where the device is connected to an AC charger, reduce the rate if the charge is over USB, and lower it further if the battery is discharging. //http://blog.csdn.net/sergeycao
Monitor Changes in Charging State
The charging status can change as easily as a device can be plugged in, so it's important to monitor the charging state for changes and alter your refresh rate accordingly.
The BatteryManager
broadcasts an action whenever the device is connected or disconnected from power. It's important to to receive these events even while your app isn't running—particularly as these events should impact how often
you start your app in order to initiate a background update—so you should register a
BroadcastReceiver
in your manifest to listen for both events by defining the
ACTION_POWER_CONNECTED
and ACTION_POWER_DISCONNECTED
within an intent filter.
<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>
Within the associated BroadcastReceiver
implementation, you can extract the current charging state and method as described in the previous step.
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 chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
}
}
Determine the Current Battery Level
In some cases it's also useful to determine the current battery level. You may choose to reduce the rate of your background updates if the battery charge is below a certain level.
You can find the current battery charge by extracting the current battery level and scale from the battery status intent as shown here:
int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
Monitor Significant Changes in Battery Level
You can't easily continually monitor the battery state, but you don't need to.
Generally speaking, the impact of constantly monitoring the battery level has a greater impact on the battery than your app's normal behavior, so it's good practice to only monitor significant changes in battery level—specifically when the device enters
or exits a low battery state.
The manifest snippet below is extracted from the intent filter element within a broadcast receiver. The receiver is triggered whenever the device battery becomes low or exits the low condition by listening for
ACTION_BATTERY_LOW
and 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>
It is generally good practice to disable all your background updates when the battery is critically low. It doesn't matter how fresh your data is if the phone turns itself off before you can make use of it.
In many cases, the act of charging a device is coincident with putting it into a dock. The next lesson shows you how to determine the current dock state and monitor for changes in device docking.
相关推荐
移动电源的充电,升压输出,电量显示程式,有电池的过充过放保护功能
可以实现对蓄电池的恒流充电与切换。对于电池保护很有效果
铅酸电池的充电模型,有了这个不用再担心不会铅酸电池充电模型了
and the battery can be charged faster. It is important to verify that a Portable Device complies with the BC 1.2 specification while communicating with a Charging Downstream Port and identifying a ...
整流器是一个整流装置,简单的说就是将交流(AC)转化为直流(DC)的装置。整流器有两个主要功能:第一,将交流电(AC)变成直流电(DC),经滤波后...第二,给蓄电池提供充电电压。因此,它同时又起到一个充电器的作用
USB2.0规范并没有考虑到使用USB接口为便携式设备的电池进行充电的需求,而这样的应用需求却越来越多。本文首先介绍USB-IF最新颁布的电池充电规范v1.2版本的主要内容;然后对其相关测试规范做简单介绍;最后结合测试经验...
电池充电模型的搭建,可以对电池进行充电同时故障时可对负载供电,可运行。
我重装了我的电脑,发现之前比较好的键盘驱动和电池管理不见了。苦寻了几天。 好像现在华硕官网不能下载了,我特此收集和整理,欢迎大家下载。 我之前只安装了电池程序发现不能调整电池保养模式。大家先安装键盘驱动...
高速锂离子电池充电硬件电路及控制程序 Driven by the need for untethered mobility and ease of use, many systems rely on rechargeable batteries as their primary power source. The battery charger is ...
Smart_Phone_Battery_Charging_FAQ
battery charging,descharging behaviour simulation in simulink appropriate to be used in standalone systems
USB-IF_电池充电规格(Battery_Charging_Specification-1.2)_2010-12-07公布
battery charging discharging model in matlab
电池充电/放电ISA
做好了一个充电电路,可以实现脉冲充电。希望多多指教,运行前在MATLAB主界面输入Ts=1e-5。
battery chnarging model
检测普通锌锰干电池的电量是否充足,通常有两种方法。第一种方法是通过测量电池瞬时短路电流来估算电池的内阻;第二种方法是用电流表串联一只阻值适当的电阻,通过测量电池的放电电流计算出电池内阻。
Battery charging applications
battery_charging_95 2.qmg
用于记录手机电池在充电,放电情况下,电池的状态信息,包括电压,百分比,充电电流等