Monitor battery usage in the application itself [closed]

1

Is there any way to put a feature in the app so it displays how much it is spending on battery / power? This would be in itself, and preventing the person from going to that config of the device that displays this, but I do not know how to start. Anyone have any ideas how to get started?

    
asked by anonymous 28.02.2018 / 14:05

1 answer

1

You can not tell how much an app is spending on battery power (1) . However it is possible to know 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 changes in load state and Track significant changes to the battery level .

References

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

    
28.02.2018 / 14:30