Android: BroadcastReceiver 不会收听 BOOT_COMPLETED

Android: BroadcastReceiver won't listen to BOOT_COMPLETED

我的应用程序推送每日通知(这工作正常)但在设备重启后通知不会再次触发。

我试图设置一个收听 BOOT_COMPLETED 的 BroadcastReceiver,但无济于事。

AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver android:name=".helpers.notification.AlarmRebootReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

广播接收器:

public class AlarmRebootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context oContext, Intent intent) {

        try {    
            Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
            notificationIntent.addCategory("android.intent.category.DEFAULT");

            String notificationMessage = TMLocale.getStringResourceByName("reminder_newthought");
            String notificationTitle = TMLocale.getStringResourceByName("app_name");

            TMNotification.Notify(notificationTitle, notificationMessage, Enum.ArtAndWordsNotification.NEWTHOUGHT, oContext);

            TMNotification.cancelNewThoughtAlarm(oContext);
            scheduleNewThoughtAlarm(oContext);
        } catch (Exception e) {
            ExceptionHandler.logException(e);
        }
    }

    private void scheduleNewThoughtAlarm(Context oContext) {

        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minutes = cal.get(Calendar.MINUTE) + 1;
        int seconds = cal.get(Calendar.SECOND);

        Calendar newCalendar = TMDate.generateCalendar(day, month, year, hour, minutes, seconds);
        TMNotification.scheduleNewThoughtAlarm(oContext, newCalendar, null);
    }
}

我在 AndroidManifest 中将 boot receiver enabled 设置为 false,并在创建警报时通过代码将其设置为 true,正如官方文档中所建议的那样(这是为了让您仅在需要时收听 boot completed ).

发出通知的地方:

private static void doNewThoughtSchedule(Context oContext, int reminderPreference, boolean randomNotificationsPreference,
                                             Calendar calendar, Intent notificationIntent){

    ComponentName receiver = new ComponentName(ApplicationContext.get(), AlarmRebootReceiver.class);
    PackageManager pm = ApplicationContext.get().getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    ...
}

显然代码中的一切都很好,但重启设备不会再次推送通知。

在华为 Android 9 设备以及较旧的 Android 4.4 phone 设备中进行了测试。 None 成功了。

有什么帮助吗?

PS。 TMNotification.scheduleNewThoughtAlarm 方法无关紧要,因为它工作正常(设置一个简单的 toast 甚至不会显示在 onReceive 中)。

PS2。这很奇怪,但是完全按照这里的官方文档也没有用:https://developer.android.com/training/scheduling/alarms.html#java(标题是 "Start an alarm when the device restarts")。

设置多个 intent-filer action 可能会受到限制。所以把你的定义改成下一个。

<!-- Don't forget about permission -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


<!-- ... -->
<receiver android:name=".helpers.notification.AlarmRebootReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

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

对于所有其他意图过滤器,请使用单独的 Receiver。现在您可以在您的方法中接收事件。只需打印日志,以确保偶数即将到来。很少有设备可以检查它不是特定于设备的。

public class AlarmRebootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context oContext, Intent intent) {
           Log.d("", "Event Received");
           /**
            * .....
            **/
    }
}