Android: How to get battery status without broadcast receiver for API level 17 and above?

I want to get the battery status. I searched for it on the stack and I found a sample code using a broadcast receiver, but the problem is that in my code, I cannot use the broadcast receiver.

So, is there a way to get battery status without using a broadcast receiver. It is possible for API 21 and up, but I want it for API 17 and up. Thanks you

+3


source to share


1 answer


Intent battery=registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

      

battery

will contain the last broadcast ACTION_BATTERY_CHANGED

Intent

, and its additional functions will contain information about the battery using the additional keys registered to BatteryManager

.



(this code will work inside a subclass method Context

and also call registerReceiver()

on another Context

)

+2


source







All Articles