I am looking for a code for estimating/guessing time remaining before the android phone battery is completely discharged. My search results were full of apps available and also the common comment that it's pretty much useless to calculate it. However i need the code. Any help, even a rough snippet, will do for getting started with.
Asked
Active
Viewed 3,689 times
3
-
1The guys below are right, you can't really guess it. But you can calculate it roughly maybe, everytime it goes down with 1%. calculate how long it took to drop 1% then look how much % their is left. and do this check every 1%? – Bigflow Nov 22 '12 at 13:58
2 Answers
2
You can not accurately guess that how much time is remaining for battery discharge, because there might be different applications or service consuming battery. thus it might vary.
However you can get battery life with help of broadcast receiver by registering a receiver for action Intent.ACTION_BATTERY_CHANGED. My answer is key only, get information from Android Developers website.
By using the below statement in onReceive() method of BroadcastReceiver with above Intent action, you will get battery level currently available(e.g., 50%, 60%, etc.). But you can't estimate the time remaining, because some apps may consume more power. So i think battery level to time remaining conversion won't give correct result.
battery_level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
I hope it may help you.

Sahil Mahajan Mj
- 11,033
- 8
- 53
- 100
-
so, I am trying to use the method of extrapolating based on the time taken say from 100% to 99% times 99 for calculating time remaining. for this i need to get battery status from the system but i am not able to get the intent ACTION_BATTERY_CHANGED. How do i make sure i get this irrespective of when it was sent. I want to get get its value the last time it was sent. I have added this in the maniest: – user1841900 Nov 23 '12 at 11:59
0
@Override
public void onCreate() {
BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
int scale = -1;
int level = -1;
int voltage = -1;
int temp = -1;
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, filter);
}

Talha
- 12,673
- 5
- 49
- 68