Search code examples
androidalarmmanager

WakefulBroadcastReceiver is not working on Nougat and Oreo devices


I have a alarm app which set alarm using Alarm Manager. Once the alarm goes off, it is received by the a receiver which starts the wakeful service which handle the alarm and allows user to stop or snooze the alarm.

below is the code snippet

public class AlarmReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, final Intent intent) {
        Alarm alarm = intent.getParcelableExtra(Constants.ARGS_ALARM);
    }
}

I am getting the proper value in older version of Android but getting alarm as null on Nougat and Oreo. My App support Nougat of minSdkVersion 17.

What could be wrong in this code?


Solution

  • Quoting myself from this blog post:

    Code that used custom Parcelable objects with AlarmManager that might have worked on older versions of Android will not work on Android N.

    Matthias Urhahn pointed out [a] workaround: convert the Parcelable into a byte[] yourself, storing that in the Bundle. Then, the OS process will just treat it as some random byte[]. This Stack Overflow answer shows the technique.

    This sample project also illustrates this technique.