WakefulBroadcastReceiver 永远不会被调用

WakefulBroadcastReceiver is never invoked

我有以下架构 BOOT_COMPLETE 意图 ->BroadcastReciever 开始,然后开始 WakefullBroadcastReceiver -> 将意图发送到 NotificationIntentService

但是我的 WakefullBroadcastReceiver 永远不会工作,如果我将架构更改为 BOOT_COMPLETE 意图 ->BroadcastReciever-> 将意图发送到 NotificationIntentService。一切都完美无缺

广播接收器:

    public class BootBroadcastReciever extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!App.isRunning) {
                Log.d("wakefull", "start");
                Intent startIntent = new Intent("SOMEACTION");
                //startIntent.setAction(Utils.NOTIFY_INTENT);
                PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0);
                AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                am.setRepeating(AlarmManager.RTC_WAKEUP,
                        SystemClock.elapsedRealtime() + 3000, 5000, startPIntent);
                App.isRunning = true;
            }
            Log.e("bool",App.isRunning+"");
        }
    }

WakefulBroadcastReceiver:

public class SimpleWakefulReciever extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("wakefull","received");
        Intent service = new Intent(context, NotificationWakefulIntentService.class);
        startWakefulService(context,service);
    }
}

NotificationIntentService:

public class NotificationWakefulIntentService extends IntentService {

    public NotificationWakefulIntentService() {
        super("NotificationWakefulIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("time",(System.currentTimeMillis()/1000)+"");
        SimpleWakefulReciever.completeWakefulIntent(intent);
    }
}

清单:

<receiver android:name=".broadCastReciever.BootBroadcastReciever" android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="START"/>
            </intent-filter>
        </receiver>

        <receiver android:name=".wakefullBroadcastReciever.SimpleWakefulReciever">
            <intent-filter>
                <action android:name="SOMEACTION"/>
                <action android:name="WAKEFUL"/>
            </intent-filter>
        </receiver>

        <service
            android:name=".wakefulService.NotificationWakefulIntentService"
            android:enabled="true">
            <intent-filter>
                <action android:name="NOTIFY_INTENT" />
            </intent-filter>
        </service>

首先,删除除 <action android:name="android.intent.action.BOOT_COMPLETED"/> 外的所有 <intent-filter> 元素。永远不要在组件上放置 <intent-filter> 除非您希望任意第三方应用程序在需要时启动该组件。

然后,替换:

Intent startIntent = new Intent("SOMEACTION");
PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0);

与:

Intent startIntent = new Intent(context, SimpleWakefulReciever.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);

这修复了您的 Intent 不再使用操作字符串并修复了您的 PendingIntent 以使用 getBroadcast(),因为您正在尝试触发 BroadcastReceiver

然后,将 5000 替换为至少 60000 的值,因为您不能比 Android 5.1+ 更频繁地重复警报。