将ArrayList <Object>从BroadCastReceiver发送到Activity

Ali*_*lin 5 android arraylist broadcastreceiver parcelable android-activity

场景:

  • 我有一个警报计划在指定的时间内运行.每次执行,我的BroadCastReceiver都会触发.

  • 在BroadCastReceiver中,我进行了所有类型的检查,最终得到了Notify类的ArrayList

  • 我在状态栏上显示通知

  • 当用户点击通知时,我会显示一个活动.我需要在我的Activity中,ArrayList在视图上显示它.

以下是示例代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

   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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

在testArrayList = b.getParcelableArrayList("list"); 来自NotificatioNActivity我收到此错误:

E/AndroidRuntime(14319): java.lang.RuntimeException: Unable to start activity
Run Code Online (Sandbox Code Playgroud)

ComponentInfo {NotificationViewer}:java.lang.RuntimeException:Parcel android.os.Parcel@4050f960:在偏移量124处解组未知类型代码7602277

正如你所看到的,从SO的qustions我说我需要让我的对象Parcelable.也许我在那里做错了但是...我不知道如何解决它.我究竟做错了什么 ?

Vin*_*kla 5

NotificationViewer活动中获取如下值:

ArrayList<Notify> testArrayList = getIntent().getParcelableArrayListExtra("list");
Run Code Online (Sandbox Code Playgroud)

您使用的是值putParcelableArrayListExtra(),因此您需要使用来获取值

getParcelableArrayListExtra()代替getParcelableArrayList()