【问题标题】:Get battery voltage获取电池电压
【发布时间】:2013-10-29 21:00:56
【问题描述】:

有没有办法可以通过系统调用来获取 Android 中的实际电池电压? 我希望这样做:

double voltage = getVoltage();
//run code
double newVoltage = getVoltage();

谢谢

编辑 - 工作代码:

fun getVoltage(context: Context): Int {
    val intent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
    return intent?.getIntExtra(android.os.BatteryManager.EXTRA_VOLTAGE, -1) ?: -1
}

【问题讨论】:

    标签: android


    【解决方案1】:

    查看此响应中 BroadcastReceiver 类中 BatteryManager.EXTRA_VOLTAGE 的使用情况:Android Battery in SDK

    【讨论】:

      【解决方案2】:

      Android 有一个 BatteryManager 类,可让您获取有关电池使用情况的信息。有一个常数 EXTRA_VOLTAGE 可能是您正在寻找的。​​p>

      http://developer.android.com/reference/android/os/BatteryManager.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        • 1970-01-01
        • 2014-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多