在不同的进程中将自定义对象传递给android服务

我有一个服务,设置为在一个单独的过程中启动:
<service android:name=".services.UploadService"
          android:process=":UploadServiceProcess" />

我可以使用bindService()成功绑定到它.当我尝试通过调用Messenger.send()发送消息时,我的问题出现了:

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

其中uploadRequest是一个实现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];
    }
};

}

我在我的服务handleMessage中设置了一个断点,但我的应用程序永远不会到达断点.但是,如果不是使用我的自定义UploadRequest对象而是发送null,我会像我期望的那样到达handleMessage断点,但显然我不能做任何事情.我在调用writetoParcel时验证了file.getPath(),返回非空字符串.这让我相信我的UploadRequest课程中有些东西已经关闭,但是通过谷歌搜索,我看不出我班级有什么问题.有任何想法吗?

解决方法

Message member obj的文档说:

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.

我的猜测是你遇到了一个问题,因为你正在创建自己的parcelable,这在穿越过程边界时是不允许的.相反,您必须将对象打包成一个包.这也意味着您的对象需要实现Serializable,但不需要是Parcelable.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...