【问题标题】:WakefulBroadcastReceiver is never invoked从不调用 WakefulBroadcastReceiver
【发布时间】:2016-10-22 18:44:44
【问题描述】:

我有以下架构 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>

【问题讨论】:

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


    【解决方案1】:

    首先,除去包裹&lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt; 之外的所有&lt;intent-filter&gt; 元素。切勿将&lt;intent-filter&gt; 放在组件上,除非您希望任意第三方应用随时启动该组件。

    然后,替换:

    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+ 更频繁地重复警报。

    【讨论】:

    • 非常感谢!现在它可以工作了,我正在使用 android 版本 15 的设备上进行测试,只是为了避免等待一分钟报警。
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多