Monitoring the Battery Android

Mobile devices operate with the use of the battery. Although many applications do not need to know the state of the battery, some types of applications might want to change their behavior based on the battery level, charging state or power management settings. For instance, a monitoring application can reduce the monitoring frequency when the battery is low and can increase it if the handset is powered by an external power source. The battery levels could also monitor the efficiency of an application and find areas where behavior can be modified to improve battery life, which is appreciated by users.

To monitor the battery, the application must have the BATTERY_STATS permission. The following XML added to the AndroidManifest.xml file is sufficient:

<uses-permission
android:name="android.permission.BATTERY_STATS" />

Then the application needs to register for a particular BroadcastIntent. In this case, it must be Intent.ACTION_BATTERY_CHANGED. The following code demonstrates this:

registerReceiver(batteryRcv, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

Diagram showing behavior flow for a Bluetooth application on Android.

Diagram showing behavior flow for a Bluetooth application on Android

Next, the application needs to provide an implementation of the BroadcastReceiver. The following is an example of a BroadcastReceiver:

batteryRcv = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
int level =
intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int maxValue =
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int batteryStatus =
intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int batteryHealth =
intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
int batteryPlugged =
intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
String batteryTech =
intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
int batteryIcon =
intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1);
float batteryVoltage =
(float) intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,
-1) / 1000;
boolean battery =
intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT,
false);
float batteryTemp =
(float) intent.getIntExtra(
BatteryManager.EXTRA_TEMPERATURE, -1) / 10;
int chargedPct = (level * 100)/maxValue ;
String batteryInfo = "Battery Info: Health=" +
(String)healthValueMap.get(batteryHealth)+" " +
"Status="+(String)statusValueMap.get(batteryStatus)+" " +
"Charged % = "+chargedPct+"% "+
"Plugged = " + pluggedValueMap.get(batteryPlugged) + " " +
"Type = " + batteryTech + " " +
"Voltage = " + batteryVoltage + " volts " +
"Temperature = " + batteryTemp + "°C "+
"Battery present = " + battery + " ";
status.setText(batteryInfo);
icon.setImageResource(batteryIcon);
Toast.makeText(Battery.this, "Battery state changed",
Toast.LENGTH_LONG).show();
}
};

There are a couple of interesting items here. First, notice that the battery level isn’t used directly. Instead, it’s used with the scale, or maximum value, to find the percentage charged. The raw value wouldn’t have much meaning to the user. The next property is the status. The values and what they mean are defined in the android.os.BatteryManager object. This is typically the charging state of the battery. Next, the health of the battery, also defined in the android.os.BatteryManager object, is an indication of how worn out the battery is. It can also indicate other issues, such as overheating. Additionally, the plugged value indicates whether the device is plugged in and, if it is, whether it is using AC or USB power.

Some other information is returned as well, including an icon identifier that can visually display the state of the battery and some technical details, such as the type of battery, current voltage, and temperature. All displayed, this information looks something like what is shown in Figure.

Screen capture showing values from the battery monitor from a physical handset.

Screen capture showing values from the battery monitor from a physical handset.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics