Optimized Battery Life for Android devices-(1) monitor Battery and Battery charging

Source: Internet
Author: User

When you want to reduce the update frequency of the background Update Service to reduce the impact of programs on the battery, checking the current battery power and charging status is a good start point.

The impact of program update on the battery depends on the current battery power and charging status. For example, when the device is charging, the update will have a negligible impact on the battery, so this is the best time for your program to increase the intensity of updates. In contrast, if the current device is not in the charging status, it is necessary to reduce the program update intensity to extend the endurance.

Similarly, if the device is not in the charging status and the battery is about to run out, it is best to minimize or even stop the update.

Determine the battery status

The current BatteryManager sends broadcast notifications to relevant broadcast recipients. I personally think it should be a broadcast sent through the sendStickyBroadcast method. This broadcast feature is that after the broadcast is sent, the broadcast entity Intent will always exist, meaning, later, when you call registerReceiver to register a Matched Receiver, this Intent object will be directly returned to the newly registered Receiver. It is important to understand this.

Because BatteryManager broadcasts an intent entity of sticky, this means that you do not have to register a broadcast receiver for your program to accept the broadcast. You can simply call the registerReceiver method, when you need to add the parameter of the broadcast receiver location to upload to null, you can also create a new broadcast receiver and import it when registering the broadcast receiver. The following code is used:


[Java]
// Sets the intent filter.
IntentFilter ifilter = new IntentFilter (Intent. ACTION_BATTERY_CHANGED );
// Register the broadcast receiver. Because BatteryManager sends intent in sticky format, the receiver can be empty.
Intent batteryStatus = registerReceiver (null, ifilter );
// Obtain the current Battery status (5 types, including unkonwn, charging, discharging, not charging, and full)
Int status = batteryStatus. getIntExtra (BatteryManager. EXTRA_STATUS,-1 );
// Whether the instance is in the charging status
Boolean isCharging = status = BatteryManager. BATTERY_STATUS_CHARGING;
// Charging method (two methods are available: AC charging and USB port charging)
Int chargePlug = batteryStatus. getIntExtra (BatteryManager. EXTRA_PLUGGED,-1 );
Boolean usbCharge = chargePlug = BatteryManager. BATTERY_PLUGGED_USB;
Boolean acCharge = chargePlug = BatteryManager. BATTERY_PLUGGED_AC;
// Print related information
Log. I ("MyBroadcastReceiverActivity", "isCharging:" + isCharging + "\ nusbCharge:" + usbCharge + "\ nacCharge:" + acCharge );
Note that the simulator is used. The result is as follows:


 


The best time to update the program is: when the battery status is AC charging, when the USB port is used for charging, the update frequency should be reduced, when not in the power-on status, it is also necessary to reduce the frequency of program updates.

Listen for changes in battery status www.2cto.com

The battery status changes when the device is powered on and started charging. Therefore, it is very important for programmers to update the battery status by listening to changes in the battery status.

BatteryManager sends a broadcast when the device is connected to the power supply or disconnected from the power supply. Even if your program is not in the running status at this time, it is also necessary to receive the information about the changes in the battery status, because you can control whether to perform corresponding update operations in the background, therefore, you should register a broadcast receiver for your program to listen for power-on and power-off statuses. Here, we use AndroidManifest. xml to register the broadcast receiver. The details are as follows:

Register a broadcast receiver named PowerConnectionReceiver in AndroidManifest. xml:

[Java]
<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>
</Cycler>

The PowerConnectionReceiver class. For details about the method, see the preceding section:

[Java]
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 = BatteryManager. BATTERY_PLUGGED_USB;
Boolean acCharge = chargePlug = BatteryManager. BATTERY_PLUGGED_AC;
Log. I ("PowerConnectionReceiver", "isCharing:" + isCharging + "\ nusbCharge:" + usbCharge + "\ nacCharge:" + acCharge );
}
}


Determine current battery power

Sometimes, it is also necessary to determine the current battery power. You can reduce the program frequency or stop updating the program when the battery power is lower than a certain value. The related code is as follows:

[Java]
// Obtain the current battery power
Int level = batteryStatus. getIntExtra (BatteryManager. EXTRA_LEVEL,-1 );
// Obtain the battery capacity
Int scale = batteryStatus. getIntExtra (BatteryManager. EXTRA_SCALE,-1 );
// Calculate battery usage
Float batteryPct = level/(float) scale;
Log. I ("MyBroadcastReceiverActivity", "level:" + level + "\ nscale:" + scale + "\ nbatteryPct:" + batteryPct );

Note that the simulator is still used. The result is as follows:


 

Monitor major changes in battery status

In general, if you keep monitoring the battery status, it may have a great impact on the battery itself, or even exceed the influence of the program itself, which is a little worth the candle, therefore, it is necessary to monitor only major changes in the battery status, such as when the device enters a low battery state or, the following code snippet listens to the two cases as follows:


[Java] print? <Cycler android: name = ". batterylevelpolicer">
<Intent-filter>
<Action android: name = "android. intent. action. ACTION_BATTERY_LOW"/>
<Action android: name = "android. intent. action. ACTION_BATTERY_OKAY"/>
</Intent-filter>
</Cycler>
<Cycler android: name = ". batterylevelpolicer">
<Intent-filter>
<Action android: name = "android. intent. action. ACTION_BATTERY_LOW"/>
<Action android: name = "android. intent. action. ACTION_BATTERY_OKAY"/>
</Intent-filter>
</Cycler>
In the onReceive sending of the broadcast receiver batterlevelpolicer class, you can do what you want.

 

 

The next section describes how to determine and listen to the dock status and type of the device.

 

From android development of yaozq

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.