0

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 plain Strings

  • 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<String> notifications = new ArrayList<String>();

         //do the checks, for exemplification I add these values
         notifications.add("This is very important");
         notifications.add("This is not so important");
         notifications.add("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);
         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);

                   //HERE I NEED the ArrayList<String> notifications
    }

I tried most of the things I have found around, from bundles to putStringArrayListExtra() but nothing worked. In my Activity I can't find a way to retrieve the data. Please help me as I am stuck.

Alin
  • 14,809
  • 40
  • 129
  • 218

3 Answers3

1

As per my suggestion, you may quickly receive it by two ways:

  1. Create your custom class which implements Percelable and design your own implementation to Write and Read parcelable object.
  2. Use Gson to serialize an object into single json string, wrap it inside an intent, receive it at other end and de-serialize the Json string back into your desired object type

there might be some other ways, like Serializing the ArrayList into bytes and write it in a file and read it later, but these two are the best ways I can recommend you to handle any type of information. Personally, I like the second one, using Gson to let it handle everything itself.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
1

According to the answer marked as the solution HERE if you do not specify an action for your pending intent the extras will not be propagated

Community
  • 1
  • 1
dymmeh
  • 22,247
  • 5
  • 53
  • 60
0

Based on all comments the working solution is this:

on BroadCastReceiver

Intent intentNotif = new Intent(context, NotificationViewer.class);
intentNotif.putStringArrayListExtra("list", notifications);

on Activity

Bundle b = getIntent().getExtras();
if (b != null) {
    testArrayList= b.getStringArrayList("list");
}

This seems to work just fine.

Alin
  • 14,809
  • 40
  • 129
  • 218
  • Ah, good point. I thought `putStringArrayListExtra` was only available with later APIs -I was obviously thinking of something totally different. – Squonk Mar 12 '12 at 20:06