发布于2021-06-12 09:26 阅读(1025) 评论(0) 点赞(3) 收藏(5)
我正在编码和 android 应用程序,它们有 2 个进程,一个显然是处理所有 UI 事物和其他东西的主进程,另一个进程托管一个处理网络(套接字)侦听和其他事物的服务。
为了在这两个进程之间进行通信,我使用了 Messenger 方法,并且在需要发送自定义 java 对象之前效果很好。这个 java 类实现 Parcelable 接口,它允许 Messenger 跨进程边界发送 java 对象。
这是我要发送的 Java 类的示例。
package com.locator.carlocator.models;
import android.os.Parcel;
import android.os.Parcelable;
public class Phone implements Parcelable {
public String number;
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.number);
dest.writeString(this.name);
}
public Phone() {
}
private Phone(Parcel in) {
this.number = in.readString();
this.name = in.readString();
}
public static final Parcelable.Creator<Phone> CREATOR = new Parcelable.Creator<Phone>() {
@Override
public Phone createFromParcel(Parcel source) {
return new Phone(source);
}
@Override
public Phone[] newArray(int size) {
return new Phone[size];
}
};
}
这就是我创建要发送的对象实例的方式,这是由接收到的消息触发的,然后发送到主进程。下一段代码运行在一个服务上,该服务运行在不同的进程上,并使用 Messenger IPC 内置 API 进行通信。
Phone phone = new Phone();
phone.setNumber("6000-0006");
phone.setName("Alex Sanchez");
data = new Bundle();
data.setClassLoader(Phone.class.getClassLoader());
data.putParcelable("key-phone", phone);
Message msg = Message.obtain(null, MSG_A_REQUEST);
msg.setData(data);
notifySubscribers(msg); // this method perform the send message.
发件人方法是如何实现的?
private void notifySubscribers(Message msg) {
for(Messenger subscriber : serviceSubscribers) {
try {
subscriber.send(msg);
} catch (RemoteException e) {
Log.e(TAG, "Subscriber not available");
e.printStackTrace();
}
}
}
这是堆栈跟踪的一部分:
00:53:05.382 21542-21542 E/Parcel: Class not found when unmarshalling: com.locator.carlocator.models.Phone
java.lang.ClassNotFoundException: com.locator.carlocator.models.Phone
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
at java.lang.Class.forName(Class.java:273)
at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Parcel.readValue(Parcel.java:2152)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:755)
at com.locator.carlocator.MainActivity$IncomingHandler.handleMessage(MainActivity.java:2255)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.locator.carlocator.models.Phone" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/221775/8479e3b9799709f19561/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!