永远不会调用WakefulBroadcastReceiver

时间:2016-10-22 18:44:45

标签: android android-intent broadcastreceiver android-broadcast android-wake-lock

我有以下架构BOOT_COMPLETE意图 - > BroadcastReciever启动,然后启动WakefullBroadcastReceiver - >将意图发送给NotificationIntentService

但如果我将架构更改为WakefullBroadcastReceiver intent - > BOOT_COMPLETE - >我的BroadcastReciever永远不会正常工作向NotificationIntentService发送意图。 everething工作得很好

广播接收器:

    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>

1 个答案:

答案 0 :(得分:1)

首先,删除除<intent-filter>之外的所有<action android:name="android.intent.action.BOOT_COMPLETED"/>元素。除非您希望任意第三方应用程序随时启动该组件,否则切勿在组件上放置<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 +更频繁地重复警报。

相关问题