【问题标题】:Android : Is there anyway to get battery capacity of a device in mah?Android : 有没有办法以 mah 获得设备的电池容量?
【发布时间】:2014-03-07 06:49:19
【问题描述】:

我想获取设备的电池容量来进行一些电池消耗计算,是否有可能以某种方式获取它?例如,三星 Galaxy Note 2 的电池容量为 3100 mAh。感谢您的帮助。

【问题讨论】:

    标签: android battery


    【解决方案1】:

    知道了!在 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();
        } 
    }
    

    【讨论】:

    • @AakashGandhi,它确实对我有用。到目前为止,它已经在我测试过的所有 Android 版本上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多