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.
Asked
Active
Viewed 7,239 times
1 Answers
20
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();
}
}

Vipul J
- 6,641
- 9
- 46
- 61
-
@AakashGandhi, It does work for me. So far it's been working on all Android versions I have tested. – Darpan May 18 '18 at 07:06