我无法接收广播的电池状态的变化?电池、状态

2023-09-05 01:39:12 作者:今年夏天我们要散了

我有完全相同的问题,因为这个帖子:电池广播接收机无法正常工作。但似乎没有人回答了这个问题。

I am having the exact same problem as this post: Battery broadcast receiver doesn't work. But it seems no one has answered that question.

下面是我的BroadcastReceiver的:

Here is my BroadcastReceiver:

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();
        }

这里是我的Andr​​oidManifest.xml:

And here is my 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>

我还添加了此行的清单:

I have also added this line to the manifest:

<uses-permission android:name="android.permission.BATTERY_STATS"/>

但仍然没有成功!

But still no success!

我真的AP preciate如果有人可以告诉我什么,我做错了。

I would really appreciate if someone could advise me what I am doing wrong.

推荐答案

从为ACTION_BATTERY_CHANGED文档:

您无法通过舱单申报组件收到此,只因为它明确地注册与Context.registerReceiver()。见ACTION_BATTERY_LOW,ACTION_BATTERY_OKAY,ACTION_POWER_CONNECTED和ACTION_POWER_DISCONNECTED为发送,并且可以通过舱单接收机接收不同电池相关的广播。

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 code

There you have it: you must explicitly register for it from your Java code.

 
精彩推荐
图片推荐