Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing custom object to android service in different process

I have a service that is set to start in a separate process:

<service android:name=".services.UploadService"
          android:process=":UploadServiceProcess" />

And I can successfully bind to it using bindService(). My issue occurs when I try to send a message by calling Messenger.send():

service.send(Message.obtain(null, UploadService.MESSAGE_UPLOAD_REQUEST, uploadRequest));

where uploadRequest is a custom object that implements Parcelable

public class UploadRequest implements Parcelable {
    public File file;
    public boolean deleteOnUpload;

public UploadRequest(File file, boolean deleteOnUpload) {
    this.file = file;
    this.deleteOnUpload = deleteOnUpload;
}

private UploadRequest(Parcel in) {
    this.file = new File(in.readString());
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.file.getPath());
}

public static final Parcelable.Creator<UploadRequest> CREATOR = new Parcelable.Creator<UploadRequest>() {
    public UploadRequest createFromParcel(Parcel in) {
        return new UploadRequest(in);
    }
    public UploadRequest[] newArray(int size) {
        return new UploadRequest[size];
    }
};

}

I set a breakpoint in my services handleMessage, but my app never gets to the breakpoint. However, if instead of using my custom UploadRequest object I send null, I get to the handleMessage breakpoint like I'd expect, but obviously I can't do anything at that point. I've verified file.getPath() when calling writeToParcel returns a non null String. This leads me to believe that something is off in my UploadRequest class, but from googling I can't see anything wrong with my class. Any ideas?

like image 563
Nelson Monterroso Avatar asked Feb 24 '11 04:02

Nelson Monterroso


People also ask

Can you pass in objects between activities Android?

The most popular ways of passing objects between activities turned out to be using either Serializable or Parcelable implementation for the object.

Can we pass object through intent in Android?

One way to pass objects in Intents is for the object's class to implement Serializable. This interface doesn't require you to implement any methods; simply adding implements Serializable should be enough. To get the object back from the Intent, just call intent.

How can I transfer data from one activity to another in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.


1 Answers

The documentation of Message member obj says:

An arbitrary object to send to the recipient. When using Messenger to send the message across processes this can only be non-null if it contains a Parcelable of a framework class (not one implemented by the application). For other data transfer use setData(Bundle). Note that Parcelable objects here are not supported prior to the FROYO release.

My guess is you are seeing an issue because you are creating your own parcelable which is not allowed when crossing the process boundary. Instead, you'll have to package your object into a bundle. This also means your object will need to implement Serializable but will not need to be Parcelable.

like image 127
Nick Campion Avatar answered Sep 28 '22 06:09

Nick Campion