passing custom object to android service in different process

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.


tried this just now.

Message.obj can transfer a framework class, such as ContentValues.

and Message.SetData can transfer a Bundle across processes and you can put any Parcelable objects into the bundle.

just remember to call setClassLoader on bundle, when received the Messag.

send side

        Message localMsg = Message.obtain();
        localMsg.what = TPServiceConnection.MSG_REPLY_SERVICE_HELLO;

        Bundle data = new Bundle();

        ContentValues cv = new ContentValues();
        cv.put("KEY", mRand.nextInt());
        data.putParcelable("KEY", cv);

        TPServiceDataModal modal = new TPServiceDataModal(mRand.nextInt());
        data.putParcelable("KEY2", modal);

        localMsg.setData(data);

receive side

        Bundle data = msg.getData();
        data.setClassLoader(this.getClass().getClassLoader());

        Parcelable parcelable = data.getParcelable("KEY");
        if (parcelable instanceof ContentValues) {
            ContentValues cv = (ContentValues) parcelable;
            Log.d(TAG, "reply content: " + cv.getAsInteger("KEY"));
        }

        Parcelable parcelable2 = data.getParcelable("KEY2");
        if (parcelable2 instanceof TPServiceDataModal) {
            TPServiceDataModal modal = (TPServiceDataModal) parcelable2;
            Log.d(TAG, "reply modal: " + modal.mData);
        }

where TPServiceDataModal is a Parcelable calss.