5

Is there a way I can make a system call to get the actual battery voltage in Android? I wish do to something like this:

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

Thanks

EDIT - Working code:

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
}
Greelings
  • 4,964
  • 7
  • 34
  • 70
fredcrs
  • 3,558
  • 7
  • 33
  • 55

2 Answers2

1

Check out the usage of BatteryManager.EXTRA_VOLTAGE within a BroadcastReceiver class in this response: Android Battery in SDK

Community
  • 1
  • 1
jdev
  • 729
  • 2
  • 13
  • 36
0

Android has a BatteryManager class that allows you to get information about battery usage. There's a constant EXTRA_VOLTAGE that may be what you are looking for.

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

Alex
  • 1,100
  • 1
  • 9
  • 23