androidbattery

Android : Is there anyway to get battery capacity of a device in mah?


I want to get battery capacity of a device to do some battery consumption computation, is it possible to get it somehow? For instance, battery capacity for Samsung Galaxy Note 2 is 3100 mah. Thanks for helping.


Solution

  • Got it! Couldn't find anything straight in SDK but can be done using reflection.

    Here is the working code :-

    public void getBatteryCapacity() {
        Object mPowerProfile_ = null;
    
        final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
    
        try {
            mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    
        try {
            double batteryCapacity = (Double) Class
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");
            Toast.makeText(MainActivity.this, batteryCapacity + " mah",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }