简体   繁体   English

Android-通过广播接收器在应用程序之间发送/接收POJO

[英]Android - Sending/Receiving POJOs across Applications via Broadcast Receiver

I am doing two Android Applications. 我正在做两个Android应用程序。

  • Application 1 sends a Broadcast which has an "Extra" Custom POJO placed into it. 应用程序1发送一个广播,其中有一个“额外的”自定义POJO。
  • Application 2 receives the Broadcast and gets the "Extra"(Custom POJO) and displays it. 应用程序2接收广播,并获取“额外”(自定义POJO)并显示它。

Here is my Custom POJO in that implements Parcelable 这是我的实现POcelable的自定义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);
    }
}

Application 1 then broadcasts like this 然后,应用程序1这样广播

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 在应用程序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");
        }
    }

}

I get an error in the line 我在一行中遇到错误

Customer customer = (Customer) data.getParcelable("customer");

because Application 2 does not have a Customer class 因为应用程序2没有客户类

So I copied the Customer class of Application 1 and pasted it in Application 2 sources to remove the error. 因此,我复制了应用程序1的Customer类并将其粘贴到应用程序2的源中以消除错误。 But after running the Application 2, shows up this error. 但是在运行应用程序2之后,显示此错误。

"Class not found when unmarshalling:com.steven.app1.Customer" “解组时找不到类:com.steven.app1.Customer”

So how would I get the Customer class in Application 1 and use it in Application 2? 那么,如何在应用程序1中获得Customer类并在应用程序2中使用它呢?

Any help or suggestions would greatly be appreciated. 任何帮助或建议,将不胜感激。 Thank you very much. 非常感谢你。

Take Customer class and place it in a library project that can be referenced by the 2 app projects. 参加Customer类,并将其放置在2个应用程序项目可以引用的库项目中。 Then, to make it easier you can implement the Serializable interface in the Customer class. 然后,为了简化操作,您可以在Customer类中实现Serializable接口。 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: 您的onReceive(..)方法应类似于:

@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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM