LocalNotification with AlarmManager and BroadcastReceiver not firing up in Android O (oreo)

前端 未结 4 1823
无人共我
无人共我 2020-12-01 06:29

I\'ve got my local notifications running on androids prior to SDK 26

But in a Android O I\'ve got the following warning, and the broadcast receiver

4条回答
  •  天涯浪人
    2020-12-01 07:04

    Android O are pretty new to-date. Hence, I try to digest and provide as accurate possible information.

    From https://developer.android.com/about/versions/oreo/background.html#broadcasts

    • Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
      • Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.
    • Apps can continue to register explicit broadcasts in their manifest.

    Also, in https://developer.android.com/training/scheduling/alarms.html , the examples are using explicit broadcast, and doesn't mention anything special regarding Android O.


    May I suggest you try out explicit broadcast as follow?

    public static void startAlarmBroadcastReceiver(Context context, long delay) {
        Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        // Remove any previous pending intent.
        alarmManager.cancel(pendingIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);        
    }
    

    AlarmBroadcastReceiver

    public class AlarmBroadcastReceiver extends BroadcastReceiver {
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
        }
    
    }
    

    In AndroidManifest, just define the class as

    
    
    

提交回复
热议问题