广播接收器收不到

Broadcast receiver not receiving

我想进行广播,我的接收器应该可以接收广播,但它不工作。
我有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="utilities.dip.com.checkbattlevelstackof">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <service
        android:name=".YourService"
        android:enabled="true"
        android:exported="false"
        android:label="@string/app_name" >
    </service>

    <!-- Receivers -->
    <receiver
        android:name=".AlarmReceiver"
        android:enabled="true" />
    <receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

接收者是:

package utilities.dip.com.checkbattlevelstackof;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;



public class BootReceiver extends BroadcastReceiver {

    public static final String TAG = "BootReceiver";
    public static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(ACTION_BOOT)) {
            // This intent action can only be set by the Android system after a boot
            Log.d(TAG,"Received boot event");
            Intent monitorIntent = new Intent(context, YourService.class);
            monitorIntent.putExtra(YourService.HANDLE_REBOOT, true);
            context.startService(monitorIntent);
        }
        else{
            Log.d(TAG," Action received : " + intent.getAction());
        }
    }
}

当我进行广播时,我没有收到任何日志:

platform-tools $ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.DEFAULT -n utilities.dip.com.checkbattlevelstackof/utilities.dip.com.checkbattlevelstackof.BootReceiver
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED cat=[android.intent.category.DEFAULT] cmp=utilities.dip.com.checkbattlevelstackof/.BootReceiver }
Broadcast completed: result=0

哪里出错了?

在应用程序标签内添加接收器和服务。

那是因为您在清单中声明了外部。它基本上应该是这样的:

<application
        .....
    <receiver
        android:name=".BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

此外,在检查操作字符串时,尤其是针对默认 Android 操作,不要使用您自己的常量。而不是使用 ACTION_BOOT 常量使用 "Intent.ACTION_BOOT_COMPLETED"