12

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.

Community
  • 1
  • 1
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67

2 Answers2

7

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);
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • It is added in API 21. Can you provide the solution that also work in lower version.? – Kishan Vaghela Aug 16 '16 at 06:50
  • Only phones that have a battery fuel gauge (https://source.android.com/devices/tech/power/device.html) can report back BATTERY_PROPERTY_CURRENT_AVERAGE. For example, Nexus 5 can only provide you with BATTERY_PROPERTY_CAPACITY. – Jehy Aug 16 '16 at 10:55
2

I dont think their any way to detect charging speed: But you can do it problematically: 1) You can record charged level (percentage or number) by

int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); batteryTxt.setText(String.valueOf(level) + "%");

2) And then after some processing time you can again get the current charged level by using same code. 3) Then calculate the difference and and then you will be able to know the charging speed.

This is just the idea that how you can implement it.

Hope this will help you