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");
}
}
You have
putSerializable
in Bundle. And you are try togetSerializable
from intent.try that.
Have you tried with any other request id other than 0
for eg
There are known issues with putting custom objects into an
Intent
that is then passed toAlarmManager
orNotificationManager
or other external applications. You can try to wrap your custom object in aBundle
, as this will sometimes work. For example, change your code to:and in
AlarmAlertBroadcastReciever.onReceive()
: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 abyte[]
and put thebyte[]
into the extras.There are dozens of examples of how to do that on Stackoverflow.
I am using android Nougat so none of these answers quite worked. I ended up passing the objects in a byte array.
Then I received the Byte[]
It's a suggestion, you can try parcelabler which is more efficient and tailored for android specific needs. so get your model class parceled here. after that paste in into your project and then pass it to another activity using either bundle
or
There are 2 things I want to suggest:
Action
to your intent, such as:yourIntent.setAction("abc.xyz")
FLAG_UPDATE_CURRENT
instead ofFLAG_CANCEL_CURRENT
Hope it helps.