设备电池总容量的编程方式为mAh

11

我尝试了Android的powerprofile....我尝试了这段代码...但每次在所有设备上都给我1000个答案... 有没有其他的方法可以在Android上获取电池容量... 例如,如果移动设备容量为2000mAh,则应返回2000

public Double getBatteryCapacity() {

        Object mPowerProfile_ = null;
        double batteryCapacity = 0;
        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) {

            // Class not found?
            e.printStackTrace();
        }

        try {

            // Invoke PowerProfile method "getAveragePower" with param
            // "battery.capacity"
            batteryCapacity = (Double) Class.forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");

        } catch (Exception e) {

            // Something went wrong
            e.printStackTrace();
        }

        return batteryCapacity;
    }

1
Ranjith,但它不起作用,所以我才发帖寻求帮助。你能帮我吗? - Jay Poojara
你应该使用PowerProfile类的“getBatteryCapacity”方法,而不是“getAveragePower”方法。 - Domenico
2个回答

17

对于那些对@Yehan建议的实现感兴趣的用户,这里有一种方法可以返回总电池容量:(仅适用于API Level >= 21)

public long getBatteryCapacity(Context context) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        BatteryManager mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        Integer chargeCounter = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
        Integer capacity = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

        if(chargeCounter == Integer.MIN_VALUE || capacity == Integer.MIN_VALUE)
            return 0;

        return (chargeCounter/capacity) *100;
    }
    return 0;
}

不幸的是,由于某些原因,这种方法并不总是有效的。在这种情况下,你可以使用Java的反射API来检索com.android.internal.os.PowerProfile的getBatteryCapacity方法返回的值:

public double getBatteryCapacity(Context context) {
    Object mPowerProfile;
    double batteryCapacity = 0;
    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context.class)
                .newInstance(context);

        batteryCapacity = (double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getBatteryCapacity")
                .invoke(mPowerProfile);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return batteryCapacity;

}

源代码: https://android.googlesource.com/platform/frameworks/base/+/a029ea1/core/java/com/android/internal/os/PowerProfile.java


我能否通过JAVA API获取当前流量值? - Hitesh Danidhariya
据我所知,我认为没有办法获取当前流的值。但是我建议你在AOSP内部搜索某些特定的内容。也许我错了。我会从这里开始:https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server - Domenico
第一种解决方案会导致我的应用程序崩溃,报错信息为java.lang.SecurityException: uid *** does not have android.permission.UPDATE_DEVICE_STATS。将其添加到清单文件中会提示“权限仅授予系统应用程序”。 - soshial
1
第一种解决方案是使用标准API,但它从未产生相同的结果。在59%的容量下,实际剩余mAh容量将缓慢下降,因此总容量也将缓慢下降,并在下一个%上跳高。第二个解决方案目前处于灰名单状态,谷歌可能随时删除它。 - 3c71

2
您可以使用android.os.BatteryManager的以下属性。 BATTERY_PROPERTY_CHARGE_COUNTER可提供以微安时为单位的剩余电池容量。 BATTERY_PROPERTY_CAPACITY可提供整数百分比形式的剩余电池容量。

因此,

BATTERY_PROPERTY_CHARGE_COUNTER / BATTERY_PROPERTY_CAPACITY * 100

将给出总电池容量。 这不是精确的计算,但您将能够接近实际容量。

References: https://source.android.com/devices/tech/power/index.html#device-power http://developer.android.com/reference/android/os/BatteryManager.html


仅适用于Android 5.0及以上版本,不支持更低版本。 - Jay Poojara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接