我有一个要通过意图发送到服务类的类(下).我已经实现了Parcelable接口,但是不确定如何实际发送和检索包括对象当前状态在内的整个对象.
尤其是
@OverrIDe public voID writetoParcel(Parcel dest, int flags) { //I need this to send the entire state of the object}
和
public UrlParamsHelper(Parcel in) { //I need this to unpack the state of the object}
这是实际的课程
/* * A holder class for URL parameters */ public static class UrlParamsHelper implements Parcelable { private final httpClIEnt httpClIEnt; private final httpParams params = new BasichttpParams(); private final SchemeRegistry registry = new SchemeRegistry(); private final ThreadSafeClIEntConnManager manager; private final Uri.Builder uri = new Uri.Builder(); final httpHost host; final String urlPath; final String hostname; /* * @param hostname the hostname IE. http://www.Google.com * @param urlPath the path to the file of interest IE. /getfiles.PHP */ public UrlParamsHelper(final String hostname, final String urlPath) { httpProtocolParams.setVersion(params, httpVersion.http_1_1); httpProtocolParams.setContentCharset(params, "UTF-8"); registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80)); manager = new ThreadSafeClIEntConnManager(params, registry); httpClIEnt = new DefaulthttpClIEnt(manager, params); host = new httpHost(hostname, 80, "http"); uri.path(urlPath); this.urlPath = urlPath; this.hostname = hostname; } public UrlParamsHelper(Parcel in) { //unpack the state } public voID addqueryString(String key, String value) { uri.appendqueryParameter(key, value); } public httpGet buildGetquery() { return new httpGet(uri.build().toString()); } public httpClIEnt gethttpClIEnt() { return httpClIEnt; } public httpHost gethttpHost() { return host; } @OverrIDe public int describeContents() { return 0; } @OverrIDe public voID writetoParcel(Parcel dest, int flags) { //Parcel the entire state of the object } //Constructs the parcel again - required public static final Parcelable.Creator<UrlParamsHelper> CREATOR = new Parcelable.Creator<UrlParamsHelper>() { public UrlParamsHelper createFromParcel(Parcel in) { return new UrlParamsHelper(in); } public UrlParamsHelper[] newArray(int size) { throw new UnsupportedOperationException(); //return new UrlParamsHelper[size]; } }; }
解决方法:
在您的writetoParcel函数中,您需要将想要的状态对象写入包裹,例如:
@OverrIDepublic voID writetoParcel(Parcel dest, int flags) { dest.writeString(urlPath); dest.writeString(hostname);}
只要以相同的顺序读回对象,则按什么顺序写对象都没关系:
@OverrIDepublic UrlParamsHelper(Parcel in) { urlPath = in.readString(); hostname = in.readString();}
问题是您只能读写文档中提到的Parcel的对象类型,因此可能很难保存所有内容.
总结以上是内存溢出为你收集整理的如何在Android中正确使用Parcelable类全部内容,希望文章能够帮你解决如何在Android中正确使用Parcelable类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)