Get current milliAmps of battery?

  • Replies:3
Ngọc Hùng (Mr Bờm)
  • Forum posts: 3

Jul 4, 2016, 8:59:11 PM via Website

How to get current battery capacity in mA of android device.

I have tried this code, but it gives me value total battery capacity:

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();
}
}

Reply
Ahm AD
  • Forum posts: 5

Jul 4, 2016, 10:24:19 PM via Website

Try this, This will work:

private Object mPowerProfile_;

private static final String POWER_PROFILE_CLASS ="com.android.internal.os.PowerProfile";

mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS).getConstructor(Context.class).newInstance(this);

double batteryCapacity=(Double)Class.forName(POWER_PROFILE_CLASS).getMethod("getAveragePower", java.lang.String.class).invoke(mPowerProfile_, "battery.capacity");

Ngọc Hùng (Mr Bờm)

Reply
Ngọc Hùng (Mr Bờm)
  • Forum posts: 3

Jul 5, 2016, 6:04:53 AM via Website

Hi @Ahm AD
Your code seems to be returned the value of the total battery capacity.
I want to get value of the current battery capacity.

Reply
Ngọc Hùng (Mr Bờm)
  • Forum posts: 3

Jul 5, 2016, 6:13:32 AM via Website

Oh, "PowerProfile" gives me what each component uses when active. It contains only static information from an XML file that defines how the device behave.
How to get current battery capacity in mA?

Reply