Mark Kuczmarski Mark Kuczmarski - 26 days ago 8
Android Question

Pass Serializable Object to Pending Intent

I am trying to send a serializable object to a pending Intent. The problem is the alarm being received is returning as null. Even though the Alarm implements the serializable interface.

//AlarmService.java

Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class);
Bundle bundle = new Bundle();
bundle.putSerializable("alarm", alarm);
myIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getAlarmTime().getTimeInMillis(), pendingIntent);


The alarm being received is null.

//AlarmAlertBroadcastReceiver.java

public class AlarmAlertBroadcastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Alarm alarm = (Alarm)intent.getExtras().getSerializable("alarm");
}
}





Edit:
Some more things I've tried is as follows, but it doesn't seem to work:

//AlarmService.java

Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class);
myIntent.putExtra("alarm", alarm);
myIntent.setAction("abc.xyz");

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getAlarmTime().getTimeInMillis(), pendingIntent);


The alarm being received is null.

//AlarmAlertBroadcastReceiver.java

public class AlarmAlertBroadcastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Alarm alarm = (Alarm)intent.getExtras().getSerializable("alarm");
//Alarm alarm = (Alarm)intent.getSerializableExtra("alarm");
}
}

Answer

There are known issues with putting custom objects into an Intent that is then passed to AlarmManager or NotificationManager or other external applications. You can try to wrap your custom object in a Bundle, as this will sometimes work. For example, change your code to:

Intent myIntent = new Intent(getApplicationContext(), 
                             AlarmAlertBroadcastReciever.class);
Bundle bundle = new Bundle();
bundle.putSerializable("alarm", alarm);
myIntent.putExtra("bundle", bundle);

and in AlarmAlertBroadcastReciever.onReceive():

Bundle bundle = intent.getBundleExtra("bundle");
if (bundle != null) {
    Alarm alarm = (Alarm)bundle.getSerializable("alarm");
}

If this does not work (and this should work on most Android versions/devices, but not all, especially very new ones), you will need to convert your Serializeable object into a byte[] and put the byte[] into the extras.

There are dozens of examples of how to do that on Stackoverflow.

Comments