在不同的进程中将自定义对象传递给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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...