I'm making a app based on BatteryManager. But every BATTERY_PROPERTY
returns 0. Here's my code:
if(Build.VERSION.SDK_INT >=21){
BatteryManager bm = (BatteryManager) MainActivity.this.getSystemService(Context.BATTERY_SERVICE);
int remainingCapacityRatio = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
//print it
int batteryCapacityMicroAh = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
//print it
int dischargingTimeSeconds = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
//print it
int currentNow = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
//print it
long energyCounter = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
//print it
}
Even I use getLongProperty()
, I only get a fixed value.
I do have the BATTERY_STATS
permission
(and Intent.ACTION_BATTERY_CHANGED
, even though it doesn't need it).
And one more thing: Can anybody tell me how can i get the current battery capacity (current, not total)? Can my code above do it?
Edit 1: The first one (remainingCapacityRatio
) works, but i need the other 4 more!
Edit 2: There was a guy who ask the same question (Android BatteryManager returning 0 for all property-retrieval calls), but he didn't get the answer he wanted!