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

标签 android battery

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

最佳答案

明白了!无法直接在 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();
    } 
}

关于安卓 : Is there anyway to get battery capacity of a device in mah?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22243461/

相关文章:

java - Eclipse 3.6 在 Content Assist 期间经常停顿

javascript - 如何在 TabNavigator 上显示图标以响应 native

android - TextView 重力中的文本不起作用

android-检测低电量通知的方法

iOS 如何获取连接的蓝牙设备的电池电量

android - android 数据库的大小会影响设备的电池效率吗?

ios - uidevicebatterystatedidchangenotification 有时不工作

android - 在我的应用程序中使用 GPS、Wi-Fi 时如何节省电量?

android - 如何在 Kotlin Android 中从 JSON 中获取数据

android - 来自 Android 设备的节能视频流