<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课程中有些东西已经关闭,但是通过谷歌搜索,我看不出我班级有什么问题.有任何想法吗?
解决方法
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.