How to call computeChargeTimeRemaining , Android BatteryManager?

797 Views Asked by At

What is the proper way to call computeChargeTimeRemaining ? The method doesn't exist when I try:

BatteryManager mBatteryManager = (BatteryManager)context.getSystemService(Context.BATTERY_SERVICE);

long time = mBatteryManager.computeChargeTimeRemaining();
1

There are 1 best solutions below

1
On

The method doesn't exist when I try

Set your compileSdkVersion to 28 (or higher, if you are reading this in the distant future).

Note that this method will only be available on Android 9.0+ devices. So, unless your minSdkVersion is 28 (or higher, for those of you reading this from your flying cars), you will need to take steps to only call this method on compatible devices:

if (Build.VERSION.SDK_INT >= 28) {
  long time = mBatteryManager.computeChargeTimeRemaining();
  // TODO something good with time
}
else {
  // ¯\_(ツ)_/¯
}