1

Hi I am trying to attach an Object while set AlarmManager by using the below code,

try {

           int when = (int)testDate.getTime();

            Intent intent1 = new Intent(mEventService, AlarmReceiver.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(Contants.NOTIFICATION_DATA, (Serializable) event);
            intent1.putExtras(bundle);

            PendingIntent sender = PendingIntent.getBroadcast(mEventService, (int)when, intent1, PendingIntent.FLAG_UPDATE_CURRENT); //192837

            // Get the AlarmManager service
            AlarmManager am = (AlarmManager) mEventService.getSystemService(mEventService.ALARM_SERVICE);
            am.setExact(AlarmManager.RTC_WAKEUP, when, sender);

        } catch (ParseException e) {
            e.printStackTrace();
        }

In the Receiver class

public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) {

Event event = (Event) intent.getSerializableExtra(Contants.NOTIFICATION_DATA);
    }
}

In the 'onReceive' the getSerializableExtra data is null. But, when I pass a String value instead of an object it is working fine. Kindly help me to find out the issue.

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • `Event` is your custom class? If so then it must be implements by `Serializable`. Must check that `event` should not `null` – Piyush Aug 11 '17 at 11:49
  • @Piyush: I am serializing'(Serializable) event' and the event is not a null objct. – Vineesh TP Aug 11 '17 at 12:01

1 Answers1

4

Using custom Parcelable and Serializable classes with extras in a PendingIntent will break things like AlarmManager on Android 7.0+.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491