The method of checking and monitoring the electric quantity and charging status in Android

  • 2020-06-01 11:00:46
  • OfStack

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

Battery life affects the execution of application updates through the remaining charge and charging state. When charging on an alternating current, the impact of performing updates on the device is negligible, so in most cases, you can turn the update frequency to the fastest. If the device is not charging, reducing the frequency of updates can help extend battery life.

Similarly, you can check the battery's remaining level, and if the battery is low, you should slow down or even stop updating.

Note: updates here refer to actions such as sending heartbeat packets, or regular updates. It doesn't just mean updating the application version. If it is a user action, such as page turning and refreshing, it does not need to be processed according to the power and charging state.

Judge the current charging state

Start by judging the current charging state. BatteryManager will broadcast all battery and charging details, including charging status, through one intent.

Since this is an sticky intent, you do not need to register a broadcast receiver. Simply by calling registerReceiver, pass in an null receiver like the code snippet below, and intent in the current battery state will be returned. You can also pass in a real receiver object, but we won't be doing updates for a while, so this is not necessary.

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
// You can read the charging state , If you're charging, you can read yes usb Or alternating current
// Are you charging
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;

In general, you should maximize the frequency of background updates while charging on alternating current, reduce them when charging on usb, and lower them when not charging.

Monitor changes in charging status

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

When the charging 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 turn on the update service in the background. So, register the broadcast receiver in Androidmanifest.xml and add two action:ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED as filters.

<receiver android:name=".PowerConnectionReceiver">
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>

In the associated broadcast receiver implementation, you can read the current charging state in the same way as in step 1:

public class PowerConnectionReceiver extends BroadcastReceiver {
    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;

Judge the current remaining electric quantity

In some cases, it is also useful to judge the current amount of electricity left. If the power level is below certain levels, you may choose to reduce the frequency of background updates.
You can read the battery with the following code:

// Current remaining quantity
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
// Maximum quantity of electricity
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Percentage of electricity
float batteryPct = level / (float)scale;

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

Monitor residual charge changes significantly

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

<receiver android:name=".BatteryLevelReceiver">
  <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
  <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>

When the remaining power is seriously low, it is best to disable all background updates. The phone is turned off before you can use it, in which case it doesn't matter if the data is refreshed.
In many cases, the device is plugged into the bottom seat to recharge (well, I don't see many people paying extra for the base anyway, probably more abroad). In the next section we'll look at how to determine the current base state and listen for changes when you insert the base. Article links: https: / / www ofstack. com article / 51557. htm

Related articles: