Submit ArrayList <Object> from BroadCastReceiver to action

Scenario:

  • I have an alarm scheduled for a certain amount of time. Every time it runs, my BroadCastReceiver fires.

  • In BroadCastReceiver, I do all kinds of checks, and the result is an ArrayList class of Notify class

  • I am showing a notification in the status bar

  • When the user clicks on the notification, I show the action. I need in my Activity, an ArrayList, to display it on the views.

Here is a 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; } } 

In 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: Sending android.os.Parcel@4050f960 : Unmarshalling unknown code type 7602277 at offset 124

As you can see, from Qustions from SO, I say that I needed to make my Parcelable object. Maybe I did something wrong, but ... I do not know how to fix it. What am I doing wrong?

+4
source share
2 answers

In NotificationViewer activity retrieval values, for example:

 ArrayList<Notify> testArrayList = getIntent().getParcelableArrayListExtra("list"); 

You add values ​​with putParcelableArrayListExtra() , so you need to get the value with

getParcelableArrayListExtra() instead of getParcelableArrayList() .

+4
source

The combination of custom Parcelable and PendingIntent is more likely iffy - but, no matter how lucky, Intent is Parcelable , why not just pass the Intent that you received as an extra for Intent you wrap it in PendingIntent - do you allow Activity or Service to do the processing?

Edit : as stated in this answer from CW.

Edit : Example

Your ReceiverAlarm class will work "kind of like":

 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 NotificationViewer :

 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. } } } 
+1
source

Source: https://habr.com/ru/post/1401122/


All Articles