问题
I am doing two Android Applications.
- Application 1 sends a Broadcast which has an "Extra" Custom POJO placed into it.
- Application 2 receives the Broadcast and gets the "Extra"(Custom POJO) and displays it.
Here is my Custom POJO in that implements Parcelable
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);
}
}
Application 1 then broadcasts like this
Intent i = new Intent("com.steven.app1.RECEIVE_CUSTOMER");
i.putExtra("customer", customer);
sendBroadcast(i);
In Application 2, I would receive the broadcast of Application 1 like this
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");
}
}
}
I get an error in the line
Customer customer = (Customer) data.getParcelable("customer");
because Application 2 does not have a Customer class
So I copied the Customer class of Application 1 and pasted it in Application 2 sources to remove the error. But after running the Application 2, shows up this error.
"Class not found when unmarshalling:com.steven.app1.Customer"
So how would I get the Customer class in Application 1 and use it in Application 2?
Any help or suggestions would greatly be appreciated. Thank you very much.
回答1:
Take Customer class and place it in a library project that can be referenced by the 2 app projects. Then, to make it easier you can implement the Serializable interface in the Customer class. Something like:
public class Customer implements Serializable{
/**
* Auto generated
*/
private static final long serialVersionUID = -559996182231809327L;
private int id;
private String firstName;
private String lastName;
public Customer(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
// Add getters/setters here
}
Which is gonna be a much more simple and clean solution.
Your onReceive(..) method should look like:
@Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Customer customer = (Customer) data.getSerializable("customer");
}
}
To pass the data, pass it as a serializable extra and you should be good to go.
Hope it helps.
来源:https://stackoverflow.com/questions/25757031/android-sending-receiving-pojos-across-applications-via-broadcast-receiver