Scenario:
I have an Alarm scheduled to run on a specified amount of time. Each time is executed, my BroadCastReceiver fires.
In BroadCastReceiver I do all kind of checks and in the end it results a ArrayList of Notify class
I display an Notification on the Statusbar
When the user taps on a Notification, I display an Activity. I need in my Activity, the ArrayList to display it on views.
Here is the sample code:
public class ReceiverAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<Notify> notifications = new ArrayList<Notify>();
//do the checks, for exemplification I add these values
notifications.add(new Notify("id1","This is very important"));
notifications.add(new Notify("id2","This is not so important"));
notifications.add(new Notify("id3","This is way too mimportant"));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//init some values from notificationManager
Intent intentNotif = new Intent(context, NotificationViewer.class);
intentNotif.putParcelableArrayListExtra("list", notifications);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotif, 0);
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
And
public class NotificationViewer extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_viewer);
ArrayList<Notify> testArrayList = null;
Bundle b = getIntent().getExtras();
if (b != null) {
testArrayList = b.getParcelableArrayList("list");
}
}
And
public class Notify implements Parcelable {
public Notify(Parcel in) {
readFromParcel(in);
}
@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Notify createFromParcel(Parcel in) {
return new Notify(in);
}
public Notify[] newArray(int size) {
return new Notify[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
}
private void readFromParcel(Parcel in) {
id = in.readString();
text = in.readString();
}
public Notify(String id, String text) {
super();
this.id = id;
this.text = text;
}
/** The id. */
public String id;
/** Notification text to be displayed. */
public String text;
@Override
public int describeContents() {
return 0;
}
}
On testArrayList = b.getParcelableArrayList("list"); from NotificatioNActivity I get this error:
E/AndroidRuntime(14319): java.lang.RuntimeException: Unable to start activity
ComponentInfo{NotificationViewer}: java.lang.RuntimeException: Parcel android.os.Parcel@4050f960: Unmarshalling unknown type code 7602277 at offset 124
As you can see, from qustions from SO I say that I needed to make my object Parcelable. Maybe I did something wrong in there but... I don't know how to fix it. What am I doing wrong ?
In NotificationViewer
activity fetch values like this:
ArrayList<Notify> testArrayList = getIntent().getParcelableArrayListExtra("list");
You are putting values using putParcelableArrayListExtra()
, so you need to get the value with
getParcelableArrayListExtra()
instead of getParcelableArrayList()
.
Combining custom Parcelable
s and PendingIntent
s is kind of iffy - but, as luck may have it, Intent
is Parcelable
, why not just pass the Intent
you received as an extra to the Intent
you are wrapping in the PendingIntent
- allowing the receiving Activity
or Service
to do the processing?
Edit: As stated in this answer from CW.
Edit: Example
Your ReceiverAlarm
class would work "sort of like this":
public class ReceiverAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentNotif = new Intent(context, NotificationViewer.class);
intentNotif.putExtra("intent", intent);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotif, 0);
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
And in the NotificationViewer
activity:
public class NotificationViewer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Fetch the Intent you received in your BroadcastReceiver
Intent broadcastIntent = getIntent().getParcelableExtra("intent");
if (broadcastIntent != null) {
// Do processing previously done in the receiver here
// and create your "Notify" objects.
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With