The method of checking, monitoring and charging state in Android _android

Source: Internet
Author: User

When you change the frequency of background updates to reduce the impact of these updates on battery life, checking the current power and charging status is a good start.

Battery life affects the execution of application updates by remaining power and charging status. When charging with alternating current, the effect of the update operation on the device is negligible, so in most cases, you can adjust the update frequency to the fastest. If the device is not charging, lowering the update frequency can help extend the battery life.

Similarly, you can check the battery remaining power level, when the low power, should reduce the update frequency or even stop the update.

Note: The update here refers to an action similar to sending a heartbeat packet, or to updating the content on a regular basis. Does not simply refer to updating the application version. If the user action, such as page refresh, do not need to be based on the power and charge status processing.

determine current charging status

Start by judging the current charging status. Batterymanager will broadcast all battery and charge details through a intent, including charging status.

Because this is a sticky intent, you do not need to register a broadcast receiver. Simply by calling Registerreceiver, as the following code snippet passes into a null receiver, the current battery status intent is returned. You can also pass in a real receiver object, but we will not be able to operate the update for the time being, so this is not necessary.

Copy Code code as follows:

Intentfilter ifilter = new Intentfilter (intent.action_battery_changed);
Intent batterystatus = Context.registerreceiver (null, IFilter);
You can read the charging status, if you are charging, you can read whether it is USB or alternating current

Whether the charge
int status = Batterystatus.getintextra (Batterymanager.extra_status,-1);
Boolean ischarging = status = Batterymanager.battery_status_charging | |
Status = = Batterymanager.battery_status_full;

How to charge
int chargeplug = Batterystatus.getintextra (batterymanager.extra_plugged,-1);
Boolean Usbcharge = Chargeplug = = Batterymanager.battery_plugged_usb;
Boolean Accharge = Chargeplug = = Batterymanager.battery_plugged_ac;

Usually you should maximize the background update frequency when using alternating current charge, reduce when using USB charge, lower when not charging.

Change of monitoring charge status

The charging status is easy to change (insert/unplug the charger), so it is important to monitor the charging status and change the refresh rate.

When the charge state changes, Batterymanager will send a broadcast. It is important to receive these events, even when the application is not running, because you may need to open the update service in the background. So, register the broadcast receiver in the Androidmanifest.xml, add two action:action_power_connected and action_power_disconnected as filter.

Copy Code code as follows:

<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 associated broadcast receiver implementation, you can read the current charging state in the same way as in the previous step:
Copy Code code as follows:

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

Determine the current remaining charge

In some cases, it is also useful to determine the current remaining amount of electricity. If the charge is below certain levels, you may choose to lower the background update frequency.
You can read the power in the following code:

Copy Code code as follows:

Current remaining charge
int level = Batterystatus.getintextra (Batterymanager.extra_level,-1);
Maximum battery value
int scale = Batterystatus.getintextra (Batterymanager.extra_scale,-1);
Charge percent
Float batterypct = level/(float) scale;

Note: For the time being, I don't know why I'm running on my own machine, and scale is 100.

Monitor remaining power changes significantly

continuous monitoring of battery status is not easy, but you don't have to do that.
In general, continuous monitoring of battery power has a greater impact on the battery than the normal behavior of the app. Therefore, it is a good practice to monitor only the specified level of remaining power changes (entering or leaving a low-power state).
The receiver declared in the manifest is triggered when entering or leaving a low-power state.

Copy Code code as follows:

<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 best to disable all background updates when the remaining power is severely low. It's off before you can use the phone, and in this case, it doesn't matter if you refresh the data.
In many cases, the device is plugged into the base to charge (well, I haven't seen a few people pay extra for the base, maybe more abroad). The following section tells you how to determine the current cradle state and monitor the insertion of the cradle when changing. Article Link: http://www.jb51.net/article/51557.htm

Related Article

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.