小编Jay*_*ara的帖子

以编程方式设备的总电池容量(mAh)

我已经尝试过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;
    }
Run Code Online (Sandbox Code Playgroud)

java android battery batterymanager

6
推荐指数
2
解决办法
5272
查看次数

标签 统计

android ×1

battery ×1

batterymanager ×1

java ×1