Can i get how much time i have before the battery goes to 0%? Something using a CountDownTimer
and making a stime of how much mAh the battery consume? Someone can help me to do it?

- 1,404
- 4
- 31
- 65
2 Answers
You can not accurately guess that how much time is remaining for battery discharge, you can just make some estimation.
You can get battery life with help of broadcast receiver by registering a receiver for action Intent.ACTION_BATTERY_CHANGED
battery_level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
Edit:
What you should do, is periodically get the battery_level
and check how much battery is being consumed per minute / per hour, and make some calculations.
So if your battery is 80% an decreasing 4% per hour, you have 20 hours estimated battery life

- 11,025
- 11
- 56
- 82
-
The poster wanted to calculate the time remaining, not the percentage. – Miro Markaravanes Jul 03 '13 at 11:17
-
You can not accurately guess that how much time is remaining for battery discharge, you can just make some estimation, but it requires loads of code and being root to monitor all system processes. – Carlos Landeras Jul 03 '13 at 11:18
-
I know that, You should've mentioned that in the answer. – Miro Markaravanes Jul 03 '13 at 11:18
-
Infact i want an estimation. But i don't know how can i do it starting with `battery_level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);` – David_D Jul 03 '13 at 11:20
Short answer: No.
Long answer: Yes, you can.. But not that easily.. Because you don't know how much energy does the device consume at a time basis, you should first try to find that out and then calculate the remaining time using that data...
As you might have guessed already, it might not be that accurate at first but the more you sample
, the more accurate that data will be..
So to conclude here is what you have to do:
First when the user opens the app, because you don't have enough data about energy consumption, You should just display something like Calculating...
. Here you should decide to have a sample time. That is something like derivation
in math..
Let's say you have a sample time of 10 seconds. ( Your actual one should be bigger I guess. )
You should have a variable named something like level1
.. Save the current level to it. Wait for the amount of sample time
which is now 10 seconds.. Now save the current value to level2
.. Save the level1 - level2
product to another variable like consumed
.
Now here is the magic. You know how much power has been consumed in 10 seconds (That is the consumed
variable).. From now on you can calculate the remaining time using that data. But remember it's still one 10 second. What if that 10 seconds user was playing a heavy game or even the device was on stand by? That is why it's not accurate. But until now..
What you can do is to add the consumed
variable to an array named like sample_data
. Let the process go on and sample the consumption every 10 seconds and add it to the array. The more data you get, the more accurate your calculation will be.
Then you can easily calculate the time remaining using the average of samples in the array.. Easy Peasy :p

- 3,285
- 25
- 32
-
Ahaha yeah it's very "Easy"!!! Ok i understand what you wrote. But maybe for me it's a little bit complicated right now :) Thanks anyway – David_D Jul 03 '13 at 11:41
-
I assume you'd use battery_level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) to get your level1, level2 etc. Since this answer is not very android specific, I'll add this: If you ran such an app on the UI thread (like you normally would) it wouldn't work. It has to be a background service using the [IntentService class](https://developer.android.com/training/run-background-service/create-service.html). – Nathan Cooper Jul 03 '13 at 11:43
-
1@user2508402 Yeah maybe it's easy for me to talk but implementing it will be hard.. But at least try to do it and if you had any questions, I'm here to answer :) – Miro Markaravanes Jul 03 '13 at 11:44
-
1@NathanCooper Yeah actually. I wrote the answer to just explain the way to do it, and I didn't had time to write code for the answer. You're right. It can't be done on the UI thread. It should be in a service or something. – Miro Markaravanes Jul 03 '13 at 11:45