【问题标题】:Android - Sending/Receiving POJOs across Applications via Broadcast ReceiverAndroid - 通过广播接收器跨应用程序发送/接收 POJO
【发布时间】: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


    【解决方案1】:

    获取 Customer 类并将其放置在一个库项目中,该库项目可以被 2 个应用项目引用。然后,为了更容易,您可以在 Customer 类中实现 Serializable 接口。比如:

    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
    }
    

    这将是一个更加简单和干净的解决方案。

    您的 onReceive(..) 方法应如下所示:

    @Override
    public void onReceive(Context context, Intent intent) {
            Bundle data = intent.getExtras();
            Customer customer = (Customer) data.getSerializable("customer");
        }
    }
    

    要传递数据,请将其作为可序列化的额外内容传递,您应该一切顺利。

    希望对你有帮助。

    【讨论】:

    • 我按照您的建议将 Customer 类放在项目库中,并在两个应用程序中引用它。有效!但是我没有使用序列化,Parcelable 也可以。谢谢
    • 很高兴听到!请记住,对于未来的实现,Serializable 接口是一种更快的方法。我曾经实现 Parcelable,但它只是让维护庞大的类变得更加困难。问候。
    • 我明白了,我会看看这个。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2015-11-12
    • 1970-01-01
    相关资源
    最近更新 更多