简体   繁体   中英

Android Battery information, without registering broadcast receiver?

Is there any way to use BatteryManager (or something similar) to get information about the battery charge level and state WITHOUT registering a broadcast receiver??

I basically want to send battery info (amongst other things) to my server upon the push of a button. Just once. Is this possible??

Thanks!

On some likely Context (eg, the Activity with the button in question), call:

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

The Intent you get back will be the last broadcast for this action, as ACTION_BATTERY_CHANGED is a sticky broadcast. You would then read the extras out of the returned Intent , as if you had received it via a BroadcastReceiver . However, since you are passing null as the receiver, you are not registering to receive broadcasts over time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM