Nel*_*oso 7 android android-service
我有一个服务,设置为在一个单独的过程中启动:
<service android:name=".services.UploadService"
android:process=":UploadServiceProcess" />
Run Code Online (Sandbox Code Playgroud)
我可以使用bindService()成功绑定到它.当我尝试通过调用Messenger.send()发送消息时,我的问题出现了:
service.send(Message.obtain(null, UploadService.MESSAGE_UPLOAD_REQUEST, uploadRequest));
Run Code Online (Sandbox Code Playgroud)
其中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];
}
};
Run Code Online (Sandbox Code Playgroud)
}
Run Code Online (Sandbox Code Playgroud) }
Run Code Online (Sandbox Code Playgroud)
<service android:name=".services.UploadService"
android:process=":UploadServiceProcess" />
}
Run Code Online (Sandbox Code Playgroud)
service.send(Message.obtain(null, UploadService.MESSAGE_UPLOAD_REQUEST, uploadRequest));
我在我的服务handleMessage中设置了一个断点,但我的应用程序永远不会到达断点.但是,如果不是使用我的自定义UploadRequest对象而是发送null,我会像我期望的那样到达handleMessage断点,但显然我不能做任何事情.我在调用writeToParcel时返回非null String验证了file.getPath().这让我相信我的UploadRequest课程中有些东西已经关闭,但是通过谷歌搜索,我看不出我的班级有什么问题.有任何想法吗?
Nic*_*ion 13
Message成员obj的文档说:
要发送给收件人的任意对象.当使用Messenger跨进程发送消息时,如果它包含框架类的Parcelable(不是应用程序实现的框架类),则它只能是非null.对于其他数据传输,请使用setData(Bundle).请注意,在FROYO版本发布之前,不支持此处的Parcelable对象.
我的猜测是你遇到了一个问题,因为你正在创建自己的parcelable,这在穿越过程边界时是不允许的.相反,您必须将对象打包成一个包.这也意味着您的对象需要实现Serializable,但不需要是Parcelable.
小智 5
刚刚尝试过这个。
Message.obj可以传递一个框架类,例如ContentValues。
Message.SetData可以跨进程传输Bundle,并且您可以将任何Parcelable对象放入该bundle中。
只需记住在收到消息时在捆绑包上调用 setClassLoader 即可。
发送方
Message localMsg = Message.obtain();
localMsg.what = TPServiceConnection.MSG_REPLY_SERVICE_HELLO;
Bundle data = new Bundle();
ContentValues cv = new ContentValues();
cv.put("KEY", mRand.nextInt());
data.putParcelable("KEY", cv);
TPServiceDataModal modal = new TPServiceDataModal(mRand.nextInt());
data.putParcelable("KEY2", modal);
localMsg.setData(data);
Run Code Online (Sandbox Code Playgroud)
接收端
Bundle data = msg.getData();
data.setClassLoader(this.getClass().getClassLoader());
Parcelable parcelable = data.getParcelable("KEY");
if (parcelable instanceof ContentValues) {
ContentValues cv = (ContentValues) parcelable;
Log.d(TAG, "reply content: " + cv.getAsInteger("KEY"));
}
Parcelable parcelable2 = data.getParcelable("KEY2");
if (parcelable2 instanceof TPServiceDataModal) {
TPServiceDataModal modal = (TPServiceDataModal) parcelable2;
Log.d(TAG, "reply modal: " + modal.mData);
}
Run Code Online (Sandbox Code Playgroud)
其中 TPServiceDataModal 是 Parcelable 类。
| 归档时间: |
|
| 查看次数: |
5525 次 |
| 最近记录: |