How can I check if my Android phone is charging?

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

I read this. How do I know if the phone is charging

But this register is registered only when the state of charge of the phone changes (from charging to stopping charging, or vice versa). But what if the phone is charging when my application starts, how can I cover this case?

I searched for PowerManager and USBManager in Android. No API provides what I need.

Thanks.

+7
source share
2 answers

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

There is also an ACTION_POWER_DISCONNECTED action.

This question says that the broadcast is sticky, so it’s even available when you skip the broadcast: Android battery in the SDK

+9
source

Now it has become much easier API 23, you can check with the BatteryManger object 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 ()

0
source

All Articles