2

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 Answers1

0

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 {
  // ¯\_(ツ)_/¯
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yes, I'm using the same functionality to get it, but it always returning -1 for me in Emulator and also in the Real device. But I can see the charge time remaining in my battery settings. – Suresh Jun 26 '20 at 04:22