Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 302
Vipul J Avatar asked Mar 07 '14 06:03

Vipul J


People also ask

Can you measure the mAh of a battery?

Milliamp-hours, or mAh, are the units technicians use to measure a battery's useful operating lifetime, or how long the battery will last when it powers a normal electrical load. You can measure a battery's mAh rating using a multimeter, a resistor to act as a load and a stopwatch to measure elapsed time.

How do I know what mAh my mobile battery is?

Find battery capacity (mAh) from Android settings First, open Settings app in your phone. Scroll down to the bottom and click About option. Under Battery Information, you will see the battery capacity of your Android phone. Here you will also see the battery level and status (charging or not charging).

Does Android show battery capacity?

Open your phone's Settings app. Under "Battery," see how much charge you have left, and about how long it will last. For details, tap Battery. For a graph and list of battery use, tap Battery Usage.


1 Answers

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();
    } 
}
like image 154
Vipul J Avatar answered Sep 28 '22 08:09

Vipul J