简体   繁体   English

Android Battery信息,是否未注册广播接收器?

[英]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?? 有什么方法可以使用BatteryManager (或类似的东西)来获取有关电池电量和状态的信息,而无需注册广播接收器?

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: 在某些可能的Context (例如,带有相关按钮的Activity ),调用:

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. 您返回的Intent将是此操作的最后一次广播,因为ACTION_BATTERY_CHANGEDACTION_BATTERY_CHANGED广播。 You would then read the extras out of the returned Intent , as if you had received it via a BroadcastReceiver . 然后,您将从返回的Intent读取额外内容,就好像您是通过BroadcastReceiver收到它一样。 However, since you are passing null as the receiver, you are not registering to receive broadcasts over time. 但是,由于您将null作为接收方传递,因此您没有注册接收一段时间内的广播。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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