安卓:反正是有得到麻将设备的电池容量是多少?是有、麻将、电池容量、设备

2023-09-12 08:08:26 作者:不傷不該傷旳吢

我想获得一个设备做一些电池消耗计算的电池容量,是有可能得到它在某种程度上?举例来说,电池容量三星Galaxy Note 2是3100毫安时。感谢您的帮助。

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.

推荐答案

明白了!找不到任何直接的SDK,但可以使用反射来完成。

Got it! Couldn't find anything straight in SDK but can be done using reflection.

下面是工作code: -

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