Search code examples
javaandroidandroid-intentserializationbroadcastreceiver

Is it possible to pass custom objects between Android apps through broadcast receivers?


Lets say that there are 2 Android apps, A and B, and B wish to pass a custom object defined in Android App A to a Broadcast Receiver defined in A, which takes in an extra and cast it into a custom object as shown below.

public void onReceive(Context paramContext, Intent intent){ ... paramIntent = (CustomClass)intent.getSerializableExtra("msg_bean");

Assuming that the broadcast receiver is exported, I tried defining the custom class in App B so that I could instantiate it in B and broadcast it to the receiver like this:

    CustomClass bean = new CustomClass ();
    intent.putExtra("msg_bean", bean);
    sendBroadcast(intent);

Assuming that App A has package a.b.c and B has package d.e.f, I got the error ClassDef not found:d.e.f.CustomClass not found in App B, because the object I sent to the receiver was of the path d.e.f.CustomClass since it is defined in App B. However, the same CustomClass defined in App A has the path a.b.c.CustomClass instead. Is there any way I can manually define the classpath of the object bean so that it follows the classpath in App A?(a.b.c.CustomClass)


Solution

  • Yes you can pass you need to create your bean class like this:
    import android.os.Parcel;
    import android.os.Parcelable;
    public class CustomClass implements Parcelable {
    
        private String name;
        private int thumbNail;
    
        public CustomClass() {
        }
    
        public CustomClass(String name, int thumbnail) {
            this.name = name;
            this.thumbNail = thumbnail;
        }
    
        protected CustomClass(Parcel in) {
            name = in.readString();
            thumbNail = in.readInt();
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeInt(thumbNail);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Creator<CustomClass> CREATOR = new Creator<CustomClass>() {
            @Override
            public CustomClass createFromParcel(Parcel in) {
                return new CustomClass(in);
            }
    
            @Override
            public CustomClass[] newArray(int size) {
                return new CustomClass[size];
            }
        };
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getThumbNail() {
            return thumbNail;
        }
    
        public void setThumbNail(int thumbNail) {
            this.thumbNail = thumbNail;
        }
    
    }
    
    => how to call intent in send broadcast:
    
    CustomClass bean = new CustomClass();
    intent.putExtra("msg_bean", bean);
    sendBroadcast(intent);
    
    => How to get object in onReceive() method:
    public void onReceive(Context paramContext, Intent intent) {
    CustomClass bean = intent.getExtras().getParcelable("msg_bean");
    }