获取电池电压

fre*_*crs 5 android

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

double voltage = getVoltage();
//run code
double newVoltage = getVoltage();
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑-工作代码:

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
}
Run Code Online (Sandbox Code Playgroud)

jde*_*dev 1

在以下响应中查看 BroadcastReceiver 类中 BatteryManager.EXTRA_VOLTAGE 的用法:Android Battery in SDK