How to test battery charging speed?

Kishan Vaghela picture Kishan Vaghela · Aug 12, 2016 · Viewed 9.2k times · Source

How can I detect battery charging speed in android device? I can detect battery status using below code. but not charging speed.

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
BatteryChangeReceiver receiver = new BatteryChangeReceiver(subscriber);
registerReceiver(receiver, filter);


public class BatteryChangeReceiver extends BroadcastReceiver {

    @Override public void onReceive(Context context, Intent intent) {
              // here I get all battery status.
    }
  }

I also refer this,this and this but cant find the way to check charging speed.

There is an app (Ampere) that displaying charging speed. enter image description here enter image description here How can I achieve this?

Thanks in advance.

Answer

Manuel Allenspach picture Manuel Allenspach · Aug 16, 2016

Use the BatteryManager. It provides the property BATTERY_PROPERTY_CURRENT_AVERAGE, which gives you the average battery current in microamperes. Unfortunately, the time period over which this information is collected may differ from device to device. Negative values mean discharging, positive values mean the phone is charging.

Alternatively, you could use BATTERY_PROPERTY_CURRENT_NOW (it looks like Ampere is using this approach). It returns the instantaneous battery current. It may not be accurate, so calling this property a couple times and calculating the average seems like a good idea.

Example code:

BatteryManager batteryManager = (BatteryManager) Context.getSystemService(Context.BATTERY_SERVICE);
int averageCurrent = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);