【问题标题】:Send ArrayList<Object> from BroadCastReceiver to Activity将 ArrayList<Object> 从 BroadCastReceiver 发送到 Activity
【发布时间】:2012-03-29 16:20:33
【问题描述】:

场景:

  • 我有一个警报计划在指定的时间内运行。每次执行时,我的 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);
    }

还有

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

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

}

在 testArrayList = b.getParcelableArrayList("list");从 NotificationNActivity 我得到这个错误:

E/AndroidRuntime(14319): java.lang.RuntimeException: Unable to start activity

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

如您所见,从 SO 的问题中,我说我需要使我的对象 Parcelable。也许我在那里做错了什么,但是......我不知道如何解决它。我做错了什么?

【问题讨论】:

  • 我无法通过快速扫描发现问题。你能添加一个 if(getIntent().getExtras().contains("list")) { log.v("", "Exists"); } else { log.v("", "不存在"); } ?
  • 该示例明确键入 Creator Parcelable.Creator&lt;MyParcelable&gt; CREATOR。你不也应该这样做吗?

标签: android arraylist broadcastreceiver android-activity parcelable


【解决方案1】:

NotificationViewer 活动中获取如下值:

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

您正在使用putParcelableArrayListExtra() 输入值,因此您需要使用来获取值

getParcelableArrayListExtra() 而不是getParcelableArrayList()

【讨论】:

  • 我已经浏览了您的代码,并且遵循了与您相同的机制,并且它在我的最后工作。您需要在最后仔细调试您的代码,因为它在我的最后工作正常。可能还有其他问题。
  • 我认为 Notify 的声明方式有问题,也许我在 Parcelable 实现中遗漏了一些东西?
  • 您的 parcelable 实现没问题。您发布的代码,它没有问题。可能是其他部分有问题。
  • 我无法找到问题所在。当你说使用它时,错误仍然存​​在:03-13 12:25:48.324: E/AndroidRuntime(15185): Caused by: Unmarshalling unknown type code at android.os.Parcel.readValue(Parcel.java:1913)在 android.os.Parcel.readListInternal(Parcel.java:2092) 在 android.os.Parcel.readArrayList(Parcel.java:1536)
  • Arghhh... 是 getParcelableArrayListExtra 参数中的错字。你是对的,它现在正如你所说的那样有效。感谢您的帮助。
【解决方案2】:

组合自定义 Parcelables 和 PendingIntents 有点不确定 - 但是,幸运的是,IntentParcelable,为什么不直接将您收到的 Intent 作为额外的Intent 你正在包裹在 PendingIntent 中 - 允许接收 ActivityService 进行处理?

编辑:正如来自 CW 的 answer 所述。

编辑:示例

您的 ReceiverAlarm 课程将“有点像这样”:

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

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

【讨论】:

  • 我不太明白你的意思。能否提供一个简短的示例代码?
  • 我想我现在明白了,但是我在通知文本中没有在问题中提到的内容(因为我不认为它是相关的)我显示类似“你有 4 个通知”的内容4 是列表的大小。这意味着我不能在我的活动中使用它,因为通知已经可见。感谢您提供的代码,我会在暗示它的情况下保留它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
相关资源
最近更新 更多