Turn: Android power management topic monitoring battery power and charging status | silent Resources

Source: Internet
Author: User
Document directory
  • Obtain the current Battery status
  • Monitor changes in the charging status
  • Obtain the current battery level
  • Monitor significant changes in battery power

By checking the current battery power and charging status of the device, and changing the update frequency of background services, the power consumption can be effectively reduced. Therefore, the update frequency of your application should be dynamically adjusted based on the current battery power and charging status of the device.

In general, when the device is charging, it can increase the data update frequency of the application, because the consumption of battery power is very small than that of the charging process. On the contrary, if the battery is in normal use, the update frequency of the application should be adjusted reasonably, and the update frequency should not be updated too frequently, which will help prolong the service life of the battery.

At the same time, the application should always monitor the status changes of the device power, when the device power is insufficient, try to reduce the update frequency, or simply stop data updates.

Obtain the current Battery status

Batterymanager sends "Sticky" system broadcast, including the current Battery status and charging status in intent.

Because the broadcast type of the battery status is "Sticky", we do not need to register the corresponding broadcastreceiver. You only need to pass the null parameter when calling registerreceiver. Then, the return value of the function contains various information about the current Battery status.

Of course, you can also pass a custom broadcastreceiver, which will be described in the following sections. However, there is actually no need for it.

Sample Code:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);Intent batteryStatus = context.registerReceiver(null, ifilter);

From the returned intent, can we get the current charging status and charging type through 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;

In general, in the case of an AC charger, You can maximize the update frequency of the application, if it is in the USB charging status, appropriately reduce the update frequency, if you are not charging, you should minimize the update frequency to make proper use of power.

Monitor changes in the charging status

As a portable charging device, switching between the charging and non-charging status may be very frequent, so your application should monitor this status switch and adjust the refreshing frequency of the application at any time.

When the device is connected to the power supply for charging, or is disconnected from the charging power, the system's batterymanager will send a broadcast.

You should register and listen to such system broadcasts in the application. Even if your application is not currently running, switching these statuses will affect the update frequency of background services of your application, you should register a broadcastreceiver to listen to the two events, namely action_power_connected and action_power_disconnected.

Sample Code:

<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>

In the batterymanager worker implementation code, you can obtain the status information of the current battery, as mentioned above.

Sample Code:

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;   }}
Obtain the current battery level

In many cases, you need to obtain the current power level. When the battery power is lower than a certain level, your application should reduce the update frequency of background services to minimize the power consumption, you can use the intent obtained above to obtain the level and scale of the current power, and then calculate the percentage of the current power.

Sample Code:

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 power

Generally, we do not need to constantly monitor the status changes of the battery, but focus on the change of battery power in the vicinity of "low battery state,

The sample code shows the changes in the "Low battery state" Status of the monitored battery, which are divided into the "Low battery state" and "normal battery state.
The corresponding actions are 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>

A better solution is: when the battery power is very low, disable background Update Services for All programs. If the phone is automatically shut down because of insufficient power, at this time, the latest data is basically useless.

Generally, when an Android device is put into a dock (device base), it enters the charging status. The next lesson will introduce how to determine the current dock status and listen for changes in the dock status.

Reference Abstract:
Http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

 

0

You may like:

Android power management topic acquisition and monitoring of network connection status

Explore Android Network Programming

Air for Android source code and implementation principle analysis

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.