我有一个服务,它被设置为在单独的进程中启动:
<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 中设置了一个断点,但是我的应用程序从未到达断点。然而,如果我发送 null 而不是使用自定义的 UploadRequest 对象,我会像预期的那样到达 handleMessage 断点,但显然此时我什么也做不了。我已经验证了在调用 writeToParcel 时返回非 null 的字符串 file.getPath()。这使我相信我的 UploadRequest 类有问题,但是从谷歌搜索中我找不出我的类哪里有问题。有什么想法吗?