-3

is there a way or how to get or to know the life span of the battery? I'm making a Battery Information just like in the "Battery Doctor Saver Pro" the difference is that the life span that i need. i don't have any idea on how to do it. can anyone help me with this? THANKS for your help. :) so far this is my code without the "Life Span" that i said.

private BroadcastReceiver battery_receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isPresent = intent.getBooleanExtra("present", false);
            String technology = intent.getStringExtra("technology");
            int plugged = intent.getIntExtra("plugged", -1);
            int scale = intent.getIntExtra("scale", -1);
            int health = intent.getIntExtra("health", 0);
            int status = intent.getIntExtra("status", 0);
            int rawlevel = intent.getIntExtra("level", -1);
            int level = 0;

            Bundle bundle = intent.getExtras();

            Log.i("BatteryLevel", bundle.toString());

            if (isPresent) {
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }

                String info = "Battery Level: " + level + "%\n";

                info += ("Technology: " + technology + "\n");
                info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
                info += ("Health: " + getHealthString(health) + "\n");
                info += ("Status: " + getStatusString(status) + "\n");

                setBatteryLevelText(info + "\n\n" + bundle.toString());
            } else {
                setBatteryLevelText("Battery not present!!!");
            }
        }
    };

    private String getPlugTypeString(int plugged) {
        String plugType = "Unknown";

        switch (plugged) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            plugType = "AC";
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            plugType = "USB";
            break;
        }

        return plugType;
    }

    private String getHealthString(int health) {
        String healthString = "Unknown";

        switch (health) {
        case BatteryManager.BATTERY_HEALTH_DEAD:
            healthString = "Dead";
            break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
            healthString = "Good Condition";
            break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
            healthString = "Over Voltage";
            break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
            healthString = "Over Heat";
            break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
            healthString = "Failure";
            break;
        }

        return healthString;
    }

    private String getStatusString(int status) {
        String statusString = "Unknown";

        switch (status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            statusString = "Charging";
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            statusString = "Discharging";
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            statusString = "Full";
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            statusString = "Not Charging";
            break;
        }

        return statusString;
    }

2 Answers2

0

As I said on Get the remaining battery time available from an Android phone, you will need to get the current battery level and work out some estimate for how fast it is currently being drained. This will depend on processor load, screen brightness, what radios are active and probably a load of other factors.

Short answer, it's certainly possible to get an estimate but it won't be easy and may not be very accurate

Community
  • 1
  • 1
Jaloopa
  • 722
  • 1
  • 6
  • 21
  • 1
    It **cannot** be accurate, because you cannot *accurately* predict how the user will use his phone over the next few minutes / hours. – Stephen C Feb 02 '12 at 14:29
  • True. Although it might be possible to build up a profile of the specific user over time to get a better estimate (eg if the device is regularly left unused between 9am - noon this could potentially be factored in). Either that or do it the way laptop battery indicators do ie time based on current drain. – Jaloopa Feb 02 '12 at 14:41
  • @Jaloopa ahmm sir? is there a code on how to make an animation for battery charge? what i mean is when charging, the icon on top of the phone? thanks for the help :) – Ringgo Lacuna De Jesus Feb 07 '12 at 13:59
0

Not exactly sure what you are trying to do here, to calculate watt hours or amp hours check this Link: http://www.powerstream.com/battery-capacity-calculations.htm If you want the time left on the battery you are currently using you need to get the current battery level and then know how fast it is being drained, either by checking how fast it has been drained up until then and then making an assumption or by knowing how much current you will draw.

Best results will be achieved if you calibrate your code for the exact battery you are using

(looks like i got beaten to the answer ;) )

Bender
  • 44
  • 1
  • 1
  • 9