This question already has an answer here:
I am developing battery health app on Android Studio. I want to know how to calculate remaining time by playing music or video, browsing website, or standby. I have seen many android apps like this but can not see the source code. I want to see the sample code for calculating remaining time for playing music or video and so on. If you have developed android battery healthy app before, please share your knowledge.
The following code will help you to calculate battery level
-- Simplest and most easiest way to calculate remaining battery life :
MainActivity.java:
xml file :
You can not accurately guess that how much time is remaining for battery discharge, because there might be different applications or service consuming battery.
However you can get battery life with help of broadcast receiver by registering a receiver for action Intent.ACTION_BATTERY_CHANGED.
By using the below statement in onReceive() method of BroadcastReceiver with above Intent action, you will get battery level currently available. But you can't estimate the time remaining, because some apps may consume more power.