Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access remaining time of battery in android

Tags:

android

I am making a project in which i calculated the level of battery

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        batteryStatus = this.registerReceiver(null, ifilter);
        level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 

Using this code i come to know what is the current level of battery. I want to know, findout and display on my project, in this battery remaining level with the services that are running what is the remaining time of battery in hours and minutes and if i am listening songs and playing videos or accessing internet than what will be the remaining time of battery. what will be battery remaining time if the phone is on standby mode and etc

So any suggestions how may i calculate this??

like image 409
nawaab saab Avatar asked Oct 21 '22 08:10

nawaab saab


1 Answers

Unfortunately the solution isn't exactly a simple answer.

There is no real way to calculate how long is left on the current battery charge, apps could be using different power at different times which means that there could be deviation in the calculation of the remaining time.

The only way that I could see you generating a somewhat reliable time would be to sample the battery as much as possible and hold the time offsets with the battery amount that was discharged. After enough samples you would be able to better estimate the amount of battery that is discharged in x amount of minutes.

EDIT: To expand that out a bit, you might get an update (A) and then another two updates (B,C). These updates could have a time stored against them in a collection of some sorts, you could then have logic that calculates the average loss between times generated from the samples that have been stored.

This solution would still not give a fully accurate result.

like image 71
JamieB Avatar answered Oct 31 '22 21:10

JamieB