14

My app uses BatteryManager.BATTERY_PROPERTY_CURRENT_NOW to get the battery current of the device:

BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
int current = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW), context);

However, it does not work for example on a Samsung Galaxy Tab A. There it returns just 0.

Other apps show the current even on that device. How do they do it? What are alternatives to my method?

P1xelfehler
  • 1,122
  • 7
  • 19
  • 1
    have you tried something like this [get-battery-level-and-state-in-android](https://stackoverflow.com/questions/3291655/get-battery-level-and-state-in-android) – James Glasgow Apr 22 '19 at 10:33
  • Possible duplicate of [Getting the battery current values for the Android Phone](https://stackoverflow.com/questions/2439619/getting-the-battery-current-values-for-the-android-phone). – Martin Zeitler Apr 25 '19 at 11:34

3 Answers3

1

From your question ("returns just 0") please take a look at the documentation on getIntProperty: https://developer.android.com/reference/android/os/BatteryManager#getIntProperty(int)

If the property is not supported or there is any other error, return

(a) 0 if targetSdkVersion < VERSION_CODES.P

or

(b) Integer.MIN_VALUE if targetSdkVersion >= VERSION_CODES.P.

Konrad Lang
  • 633
  • 6
  • 9
-1

Use this method:

int getBatteryPercent(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent != null) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        return level * 100 / scale;
    }
    return 0;
}
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
-3

Try this :

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

    int current = batteryManager.getIntProperty(BatteryManager.EXTRA_LEVEL), context)
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
mit
  • 34
  • 3