Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BATTERY_PROPERTY_CURRENT_NOW not working on every device. What are alternatives?

Tags:

java

android

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?

like image 610
P1xelfehler Avatar asked Jun 26 '17 11:06

P1xelfehler


1 Answers

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.

like image 114
Konrad Lang Avatar answered Nov 13 '22 19:11

Konrad Lang