Android:无论如何要获得设备的电池容量吗?

Vip*_*l J 5 android battery

我想让设备的电池容量做一些电池消耗计算,有可能以某种方式得到它吗?例如,三星Galaxy Note 2的电池容量为3100毫安.谢谢你的帮助.

Vip*_*l J 19

得到它了!在SDK中找不到任何东西,但可以使用反射完成.

这是工作代码: -

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();
    } 
}
Run Code Online (Sandbox Code Playgroud)