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?
The most popular ways of passing objects between activities turned out to be using either Serializable or Parcelable implementation for the object.
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.
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With