【发布时间】:2014-09-10 03:51:50
【问题描述】:
我正在做两个 Android 应用程序。
- 应用程序 1 发送一个广播,其中放置了一个“额外的”自定义 POJO。
- 应用程序 2 接收广播并获取“额外”(自定义 POJO)并显示它。
这是我实现 Parcelable 的自定义 POJO
package com.steven.app1;
public class Customer implements Parcelable {
private int id;
private String firstName;
private String lastName;
public static final Parcelable.Creator<Customer> CREATOR = new Parcelable.Creator<Customer>() {
public Customer createFromParcel(Parcel in) {
return new Customer(in);
}
public Customer[] newArray(int size) {
return new Customer[size];
}
};
public Customer(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Customer(Parcel source) {
id = source.readInt();
firstName = source.readString();
lastName = source.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(firstName);
dest.writeString(lastName);
}
}
应用程序 1 然后像这样广播
Intent i = new Intent("com.steven.app1.RECEIVE_CUSTOMER");
i.putExtra("customer", customer);
sendBroadcast(i);
在应用程序 2 中,我会像这样接收应用程序 1 的广播
package com.steven.app2;
public class CustomerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Customer customer = (Customer) data.getParcelable("customer");
}
}
}
我在一行中遇到错误
Customer customer = (Customer) data.getParcelable("customer");
因为应用程序 2 没有 Customer 类
所以我复制了应用程序 1 的客户类并将其粘贴到应用程序 2 源中以消除错误。但是运行Application 2之后,就出现了这个错误。
“解组时找不到类:com.steven.app1.Customer”
那么我如何在应用程序 1 中获取客户类并在应用程序 2 中使用它?
任何帮助或建议将不胜感激。非常感谢。
【问题讨论】:
标签: java android android-intent broadcastreceiver