我有一项服务设置为在单独的进程中启动:
<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]; } };
}
我在我的服务句柄消息中设置了一个断点,但我的应用程序永远不会到达断点。但是,如果不是使用我的自定义 UploadRequest 对象我发送 null,我会像我期望的那样到达 handleMessage 断点,但显然我此时无能为力。当调用 writeToParcel 返回非空字符串时,我已经验证了 file.getPath()。这让我相信我的 UploadRequest 课程中有问题,但通过谷歌搜索我看不出我的课程有什么问题。有任何想法吗?