1

I'm making battery manager - it's almost done, but i want to have Battery estimator that will show me how much longer my battery will run my telephone.

I just want to know the simpliest way to do it, im well aware it will be only just the approximation.

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);

}

I have this line of code in my java, when i run this app on my device it show's nothing. What should i change/add here or in my xml file ?

This code is taken from : Remaining time to battery discharge on android

Community
  • 1
  • 1
Johansen
  • 11
  • 1
  • That code -- as is -- only writes a message to the log, so it's not surprising you see nothing on the device. You would have to plug the numbers it gets (or something derived from them) into your app's GUI. As the linked question emphasises, guestimating remaining life is prone to error, since you don't know what the user will do next (and therefore how long the remaining charge would last). The best you could do is keep historic data of battery-level against time to make some attempt at "based on the last 1/2/4 hour's use, the remaining battery would last xx hours". – TripeHound Sep 09 '15 at 12:08
  • Thank You for your answer, it helped me to understand current situation. I have another question : How about trying to get current voltage of the battery and after few sedonds take another, so that it would not take too much time to get the information. I just want to do something like : CLICK a button, and after few seconds it would give me answer, would it be possible to have a lapse no more than +/- 30 minutes of appropriate time ? Because on what You are saying it would need some database in it and make it more complicated and I'm not much into making database yet :/ – Johansen Sep 09 '15 at 18:51
  • There are two aspects: **(1)** measuring past use and **(2)** predicting future use. The second is, as your original link suggests, always going to be an educated guess at best. For the first, you _could_ estimate past use with just a couple of readings taken a few seconds apart, but that will only give you a "snapshot" of the use over that short time: if a CPU-hungry app or service had kicked-in during that time, it would distort the figures: a longer time between readings will average out such variations. You shouldn't need to store multiple readings... **/cont** – TripeHound Sep 10 '15 at 08:12
  • **/cont** ...Take a reading as soon as you open your app and remember this. Then take a new reading a few seconds later for a very rough, "instantaneous" measure of battery use. Periodically, take new readings and re-calculate the usage with respect to the original reading. As more time passes, the "rate of battery use" will average out to a more informative figure. – TripeHound Sep 10 '15 at 08:15

0 Answers0