Getting the battery level

Viewed 4897

I know there are many questions like this, but I need a concrete answer whether it is possible or not.

I want to get the current available battery power in mAh for any android phone. It is crucial for my project. I tried BatteryManager.

batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);

It exactly provides the information I need, but only for some devices.

I also tried using PowerProfile via Java Reflection.

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(OffloadeeActivity.this, batteryCapacity + " mAh",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But it provides the total battery capacity. What I need is the current remaining battery power. Is there any universal way to get that for every device? Any library would also do.

2 Answers

This can be achieved by getting the total battery capacity in milliamp hours, then finding the percentage of the battery level, and finally multiplying them together to get the current battery life in milliamp hours. The following block of code is a function for getting the total capacity of the battery in milliamp hours:

public double getBatteryCapacity(Context context) {
    Object mPowerProfile;
    double batteryCapacity = 0;
    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
            .getConstructor(Context.class)
            .newInstance(context);

    batteryCapacity = (double) Class
            .forName(POWER_PROFILE_CLASS)
            .getMethod("getBatteryCapacity")
            .invoke(mPowerProfile);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return batteryCapacity;
}

Now that you have the battery capacity, you can find the current percentage of battery life and then multiply it by the total capacity to get the charge in milliamp hours:

BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

If you want to get back the battery percentage level as a float, (Example 89.92%), you can use the following code:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batLevel = (float) level / (float) scale;

Now, you can do the following to get the milliamp hour battery life:

int batteryInMilliamps = getBatteryCapacity(getApplicationContext()) * batLevel;

If you receive any issues or have any questions, just comment below.

You try this it will help for you

public static int getBatteryPercentage(Context context) {

    IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, iFilter);

    int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
    int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;

    float batteryPct = level / (float) scale;

    return (int) (batteryPct * 100);
}
Related