Android-通过广播接收器跨应用程序发送/接收POJO [英] Android - Sending/Receiving POJOs across Applications via Broadcast Receiver

查看:503
本文介绍了Android-通过广播接收器跨应用程序发送/接收POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做两个Android应用程序.

I am doing two Android Applications.

  • 应用程序1发送一个广播,其中有一个额外的"自定义POJO.
  • 应用程序2接收广播,并获取附加"(自定义POJO)并显示它.

这是我的自定义POJO,其中实现了Parcelable

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

应用程序1然后像这样广播

Application 1 then broadcasts like this

Intent i = new Intent("com.steven.app1.RECEIVE_CUSTOMER");
i.putExtra("customer", customer);
sendBroadcast(i);

在Application 2中,我将收到这样的Application 1广播

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

}

该行出现错误

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

因为应用程序2没有客户类

because Application 2 does not have a Customer class

因此,我复制了应用程序1的Customer类,并将其粘贴到应用程序2的源中以消除错误.但是在运行应用程序2之后,会显示此错误.

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.

解组时找不到类:com.steven.app1.Customer"

"Class not found when unmarshalling:com.steven.app1.Customer"

那么我将如何在Application 1中获得Customer类并在Application 2中使用它?

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.

推荐答案

采用 Customer 类并将其放置在2个应用程序项目均可引用的库项目中.然后,为简化操作,您可以在 Customer 类中实现 Serializable 接口.像这样:

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.

您的 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.

希望有帮助.

这篇关于Android-通过广播接收器跨应用程序发送/接收POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆