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?
Quoting myself from this blog post:
Code that used custom
Parcelable
objects withAlarmManager
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 abyte[]
yourself, storing that in theBundle
. Then, the OS process will just treat it as some randombyte[]
. This Stack Overflow answer shows the technique.
This sample project also illustrates this technique.