Monitor battery usage in the app itself

Asked

Viewed 41 times

1

Have some way to put a feature in the app, so it shows how much it is spending on battery/power?
That would be on himself, and preventing the person from going to that config of the device that displays it, but I don’t know how to start. Does anyone have any idea how to start?

1 answer

1

Can’t tell how much an app is battery/power consuming(1). However it is possible to know what is the current battery level:

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

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale; 

whether the battery is being charged or not:

int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
boolean isCharged = status == BatteryManager.BATTERY_STATUS_FULL;

int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

See in the documentation how Monitor load status changes and Monitor significant battery level changes.

References

(1) It is not possible to know programmatically but it is possible to use the adb to dump the collected battery usage data to the pc and create a report that can be analyzed using the Battery Historian.

  • So, I came to give a read on, but it wasn’t exactly what I needed, but thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.