gpt4 book ai didi

android - 将 ArrayList 从 BroadCastReceiver 发送到 Activity
转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:15 25 4
gpt4 key购买 nike

场景:

  • 我有一个闹钟计划在指定的时间运行。每次执行时,我的 BroadCastReceiver 都会触发。

  • 在 BroadCastReceiver 中,我进行了各种检查,最后得到了一个 Notify 类的 ArrayList

  • 我在状态栏上显示通知

  • 当用户点击通知时,我会显示一个 Activity 。我需要在我的 Activity 中使用 ArrayList 在 View 上显示它。

示例代码如下:

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");从 NotificatioNActivity 我得到这个错误:

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

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

最佳答案

NotificationViewer Activity 中获取这样的值:

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

您正在使用 putParcelableArrayListExtra() 放置值,因此您需要使用

获取值

getParcelableArrayListExtra() 而不是 getParcelableArrayList()

关于android - 将 ArrayList<Object> 从 BroadCastReceiver 发送到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9681103/

25 4 0