我遇到了与这篇文章完全相同的问题:Battery broadcast receiver doesn't work .但似乎没有人回答过这个问题。
这是我的广播接收器:
public class BatteryLevelReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.v("plugg", "plug change fired");
Toast.makeText(context, " plug change fired", Toast.LENGTH_LONG).show();
}
这是我的 AndroidManifest.xml:
<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
<intent-filter android:priority="900">
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
<intent-filter android:priority="900">
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
我还在 list 中添加了这一行:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
但是还是没有成功!
如果有人能告诉我我做错了什么,我将不胜感激。
最佳答案
来自 the documentation for ACTION_BATTERY_CHANGED :
You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers.
好了:您必须从您的 Java 代码中显式注册它。
关于android - 我无法接收有关电池状态变化的广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277302/