9

I am writing an android application. I would like to know how can I check if the phone is charging when my application starts?

I have read this How to know if the phone is charging

But this only register and get notified when there is a change in charging state of the phone (from charging to stop charging or vice versa). But what if the phone is charging when my application starts, how can I cover that case?

I have looked up PowerManager and USBManager in android. Neither has API which provides what I need.

Thank you.

Community
  • 1
  • 1
michael
  • 93,094
  • 111
  • 230
  • 334

2 Answers2

10

This question provides the code you might be looking for: Event ACTION_POWER_CONNECTED is not sent to my BroadcastReceiver

There's also a ACTION_POWER_DISCONNECTED action.

This question is telling that the broadcast is sticky, so even available when you miss the broadcast: Android Battery in SDK

Community
  • 1
  • 1
Knickedi
  • 8,572
  • 2
  • 40
  • 45
  • Thanks but not. I need to know if the phone is charging when my application starts. Registering any intent only tell me when charging stage changes, but what if the phone is already plugin and charging? – michael Sep 24 '11 at 00:17
  • Ahh, I see. Check [this post](http://stackoverflow.com/questions/1805895/android-battery-in-sdk/1805922#1805922). – Knickedi Sep 24 '11 at 00:45
  • Thank you. What about the case which the phone is fully charge, but it is connected to a power adapter (usb)? in other words, the phone is not draining any batter (since it is connected to a power source) – michael Sep 28 '11 at 21:51
  • I really don't know but since you know how to catch the events you should be able to find it out easily I think. I never caught that broadcast on my own ;-) – Knickedi Sep 28 '11 at 21:55
  • 1
    Thank you. I think I can get the BatteryManager.EXTRA_PLUGGED in the intent – michael Sep 28 '11 at 22:47
5

It has become much more simple API 23 onwards, you can check using an object of BatteryManger and call the isCharging function.

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

public boolean isUSBCharging(){
   return  myBatteryManager.isCharging();
}

https://developer.android.com/reference/android/os/BatteryManager.html#isCharging()

Abhishek Dhotre
  • 371
  • 3
  • 11