传递可分割对象时“android.os.BadParcelableException:解组时为ClassNotFoundException”

时间:2017-03-16 12:47:07

标签: android badparcelableexception

将实现Parcelable的Message传递给另一个服务进程时遇到问题。

似乎我应该使用ClassLoader来解决问题?但我不知道该怎么做。

这是我的代码:

 public class BaconRequest implements Parcelable {

    private String remoteIP;
    private int remotePort;
    private long timeout;
    private int repeat;
    private String requestMsg;
    private boolean isPing;
    private boolean isLongLink;
    private boolean isTCP;


    public BaconRequest() {
    }

    public BaconRequest(String remoteIP, int remotePort, long timeout, int repeat, String requestMsg, boolean isPing, boolean isLongLink, boolean isTCP) {
        this.remoteIP = remoteIP;
        this.remotePort = remotePort;
        this.timeout = timeout;
        this.repeat = repeat;
        this.requestMsg = requestMsg;
        this.isPing = isPing;
        this.isLongLink = isLongLink;
        this.isTCP = isTCP;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(remoteIP);
        out.writeInt(remotePort);
        out.writeLong(timeout);
        out.writeInt(repeat);
        out.writeString(requestMsg);
        out.writeByte((byte) (isPing ? 1 : 0));
        out.writeByte((byte) (isLongLink ? 1 : 0));
        out.writeByte((byte) (isTCP ? 1 : 0));
    }

    public static final Parcelable.Creator<BaconRequest> CREATOR = new Creator<BaconRequest>() {
        @Override
        public BaconRequest[] newArray(int size) {
            return new BaconRequest[size];
        }

        @Override
        public BaconRequest createFromParcel(Parcel in) {
            BaconRequest baconRequest = new BaconRequest();
            baconRequest.remoteIP = in.readString();
            baconRequest.remotePort = in.readInt();
            baconRequest.timeout = in.readLong();
            baconRequest.repeat = in.readInt();
            baconRequest.requestMsg = in.readString();
            baconRequest.isPing = in.readByte() != 0;
            baconRequest.isLongLink = in.readByte() != 0;
            baconRequest.isTCP = in.readByte() != 0;
            return baconRequest;
        }
    };
}

日志:

E/Parcel: Class not found when unmarshalling: com.tencent.glensun.bacon.tcp.client.BaconRequest
                                                                            java.lang.ClassNotFoundException: com.tencent.glensun.bacon.tcp.client.BaconRequest
                                                                                at java.lang.Class.classForName(Native Method)
                                                                                at java.lang.Class.forName(Class.java:324)
                                                                                at android.os.Parcel.readParcelableCreator(Parcel.java:2404)
                                                                                at android.os.Parcel.readParcelable(Parcel.java:2358)
                                                                                at android.os.Message.readFromParcel(Message.java:571)
                                                                                at android.os.Message.access$000(Message.java:32)
                                                                                at android.os.Message$1.createFromParcel(Message.java:527)
                                                                                at android.os.Message$1.createFromParcel(Message.java:524)
                                                                                at android.os.IMessenger$Stub.onTransact(IMessenger.java:51)
                                                                                at android.os.Binder.execTransact(Binder.java:453)

Messenger ,我过去通过跨进程进行通信,在一个进程中创建一个Messenger,并将消息处理到另一个进程。此外,这两个过程都在同一个应用程序中。

以下是MAIN流程中的代码:

public class Client{
    private Messenger mServiceMessenger;
    private ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: ");
            mServiceMessenger = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
            mServiceMessenger = null;
        }
    };

    public void startService(Context context){
        context.bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);
    }

    public void send(BaconRequest request){
        mServiceMessenger.send(Message.obtain(null,1, request));
    }
}

我在SERVICE流程中的代码:

public class MyService extends Service{

    private Messenger mMessenger;

    private Handler ipcReceiveHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Log.d(TAG, "handleMessage:");
        }
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        mMessenger = new Messenger(ipcReceiveHandler);
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }

}

非常感谢!

0 个答案:

没有答案