简体   繁体   English

Android:如何在没有Broadcast Receiver for API 17级及以上的情况下获得电池状态?

[英]Android :How to get Battery status without Broadcast Receiver for API level 17 and above?

I want to get Battery status. 我想获得电池状态。 I searched it on stack and i found the sample code using Broadcast Receiver but problem is that in my code i cannot use Broadcast Receiver. 我在堆栈上搜索它,我发现使用广播接收器的示例代码,但问题是在我的代码中我不能使用广播接收器。

So is there any way to get battery status without using Broadcast Receiver. 那么有没有办法在不使用广播接收器的情况下获得电池状态。 It is possible for API 21 and above but i want it for API 17 and above. API 21及更高版本是可能的,但我希望它适用于API 17及更高版本。 Thank you 谢谢

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

battery will contain the last-broadcast ACTION_BATTERY_CHANGED Intent , and its extras will contain the battery information, using the extra keys documented on BatteryManager . battery将包含最后一次广播的ACTION_BATTERY_CHANGED Intent ,其附加内容将包含电池信息,使用BatteryManager记录的额外密钥。

(this code will work inside a method of a Context subclass; alternatively, call registerReceiver() on some other Context ) (此代码将在Context子类的方法内部工作;或者,在其他某些Context上调用registerReceiver()

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

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