通过Messenger将Parcelable传递给远程服务时解组时找不到类

Che*_*eng 13 android

我有一个Parcelable对象,我用它将它从Activity传递给远程服务.当我使用AIDL界面传递它时,一切听起来都很好.

最近,我尝试通过Activity 传递Messenger.

// TEST TEST TEST!
StockInfo stockInfo0 = new StockInfo(Code.newInstance("code0"), Symbol.newInstance("symbol0"));
StockInfo stockInfo1 = new StockInfo(Code.newInstance("code1"), Symbol.newInstance("symbol1"));
StockInfo stockInfo2 = new StockInfo(Code.newInstance("code2"), Symbol.newInstance("symbol2"));
List<StockInfo> stockInfos = new ArrayList<StockInfo>();
stockInfos.add(stockInfo0);
stockInfos.add(stockInfo1);
stockInfos.add(stockInfo2);
StockInfosEx stockInfosEx = new StockInfosEx(stockInfos, "abc");
msg.obj = stockInfosEx;

try {
    mService.send(msg);
} catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我在远程服务中遇到以下异常.

02-21 22:55:16.546:E/Parcel(8365):解组时找不到类:com.example.testonmessenger.StockInfosEx,e:java.lang.ClassNotFoundException:com.example.testonmessenger.StockInfosEx

我在想,两者之间有什么问题?这是我的Parcelable对象.

public class StockInfosEx implements Parcelable {
    public final List<StockInfo> stockInfos;
    public final String searchedString;

    public StockInfosEx(List<StockInfo> stockInfos, String searchedString) {
        this.stockInfos = stockInfos;
        this.searchedString = searchedString;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Handling Parcelable nicely.

    public static final Parcelable.Creator<StockInfosEx> CREATOR = new Parcelable.Creator<StockInfosEx>() {
        public StockInfosEx createFromParcel(Parcel in) {
            return new StockInfosEx(in);
        }

        public StockInfosEx[] newArray(int size) {
            return new StockInfosEx[size];
        }
    };

    private StockInfosEx(Parcel in) {
        stockInfos = new ArrayList<StockInfo>();
        in.readTypedList(stockInfos, StockInfo.CREATOR);
        searchedString = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeTypedList(stockInfos);
        parcel.writeString(searchedString);
    }

    // Handling Parcelable nicely.    
    ////////////////////////////////////////////////////////////////////////////        
}
Run Code Online (Sandbox Code Playgroud)

要获得完整的源代码,请从https://www.dropbox.com/s/n69yuhddpb8vedz/testonmessenger.zip下载

Che*_*eng 22

不可行的方法(因为我们的Parcelable是自定义的,不是像Rect这样的框架的一部分)

活动

msg.obj = stockInfosEx;
Run Code Online (Sandbox Code Playgroud)

远程服务

StockInfosEx stockInfosEx = (StockInfosEx)msg.obj;
Run Code Online (Sandbox Code Playgroud)

可行的方法

活动

msg.getData().putParcelable("data", stockInfosEx);
Run Code Online (Sandbox Code Playgroud)

远程服务

msg.getData().setClassLoader(StockInfosEx.class.getClassLoader());
StockInfosEx stockInfosEx = (StockInfosEx)msg.getData().getParcelable("data");
Run Code Online (Sandbox Code Playgroud)

现在,在我再次回读msg.obj(http://developer.android.com/reference/android/os/Message.html#obj)的文档之后,只有我理解了它的真正含义.Parcelable of a framework class

要发送给收件人的任意对象.当使用Messenger跨进程发送消息时,如果它包含框架类的Parcelable(不是应用程序实现的框架类),则它只能是非null.对于其他数据传输,请使用setData(Bundle).

请注意,在FROYO版本发布之前,不支持此处的Parcelable对象.


m0s*_*it0 6

你可能没有使用正确的权利ClassLoader.你需要首先跟踪ClassLoader编组类的内容,并使用ClassLoader它来解组它.

解组时,你正在使用当前线程的ClassLoader,它不是你的UIThread而是Android系统线程,因此,没有关于你的自定义类的信息.

我使用了一个包含my的静态类ClassLoader来解决这个问题(可以使用类似的方法而不需要它是静态的).

就像是:

ClassLoaderHelper.setClassLoader(Thread.currentThread().getContextClassLoader());
Run Code Online (Sandbox Code Playgroud)

然后在解组时:

public final void readFromParcel(final Parcel in) {
    id = in.readString();
    appInfo = in.readParcelable(ClassLoaderHelper.getClassLoader());
    ...
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅此其他问题(可能是重复的btw).