Android : Is there anyway to get battery capacity of a device in mah?

柔情痞子 提交于 2019-12-17 09:52:47

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/22243461/android-is-there-anyway-to-get-battery-capacity-of-a-device-in-mah

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!