如何在Android的正常使用Parcelable类正常使用、如何在、Android、Parcelable

2023-09-06 17:45:37 作者:双眼抑万郁

我有一个类(见下文),我想通过一个Intent发送给服务类。我已经实现了Parcelable接口,但我不能确定如何实际发送和检索包括对象的当前状态,整个对象。

在特别

  @覆盖
公共无效writeToParcel(包裹DEST,INT标志){
      //我需要这个发送对象的整个状态
}
 

 公共UrlParamsHelper(包裹中){
    //我需要这个解压的对象的状态
}
 
Android Coding利器之掌握小技巧,助你Coding更上一层楼

下面是实际的类

  / *
  * holder类的URL参数
  * /
 公共静态类UrlParamsHelper实现Parcelable {
  私人最终HttpClient的HttpClient的;
  私人最终的HttpParams PARAMS =新BasicHttpParams();
  私人最终SchemeRegistry注册=新SchemeRegistry();
  私人最终ThreadSafeClientConnManager经理;
  私人最终Uri.Builder URI =新Uri.Builder();
  最后HttpHost主机;

  最后弦乐urlPath;
  最后弦乐的主机名;
  / *
   *参数主机的主机名,即。 http://www.google.com
   *参数urlPath的路径,即利息的文件。 /getfiles.php
   * /
  公共UrlParamsHelper(最后弦乐主机名,最后弦乐urlPath){
   HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(PARAMS,UTF-8);
   registry.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
   经理=新ThreadSafeClientConnManager(参数,可以登记);
   HttpClient的=新DefaultHttpClient(经理,则params);
   主机=新HttpHost(主机名,80,HTTP);
   uri.path(urlPath);

   this.urlPath = urlPath;
   this.hostname =主机名;
  }

  公共UrlParamsHelper(包裹中){
   //解压状态
  }

  公共无效addQueryString(字符串键,字符串值){
   uri.appendQueryParameter(键,值);
  }

  公共HTTPGET buildGetQuery(){
   返回新HTTPGET(uri.build()的toString());
  }

  公共HttpClient的getHttpClient(){
   返回HttpClient的;
  }

  公共HttpHost getHttpHost(){
   返回主机;
  }

  @覆盖
  公众诠释describeContents(){
   返回0;
  }

  @覆盖
  公共无效writeToParcel(包裹DEST,INT标志){
   //包裹对象的整个状态
  }

  //重新构造包裹 - 要求的
      公共静态最终Parcelable.Creator< UrlParamsHelper> CREATOR =新Parcelable.Creator< UrlParamsHelper>(){
       公共UrlParamsHelper createFromParcel(包裹中){
           返回新UrlParamsHelper(中);
       }

       公共UrlParamsHelper [] newArray(INT尺寸){
        抛出新UnsupportedOperationException异常();
           //返回新UrlParamsHelper【尺寸】;
       }
   };
 }
 

解决方案

在你writeToParcel功能,你需要写你想要的状态对象的包裹,例如:

  @覆盖
公共无效writeToParcel(包裹DEST,INT标志){
    dest.writeString(urlPath);
    dest.writeString(主机名);
}
 

没关系的顺序编写的对象,只要你读他们回以相同的顺序:

  @覆盖
公共UrlParamsHelper(包裹中){
    urlPath = in.readString();
    主机名= in.readString();
}
 

现在的问题是,你只能读取和写入的文件中提到的包裹,因此可能难以挽救绝对一切。

I have a class (below) that I want to send to a Service Class through an Intent. I have implemented the Parcelable interface but am unsure how to actually send and retrieve the entire object including the current state of the object.

In particular

@Override 
public void writeToParcel(Parcel dest, int flags) {
      //I need this to send the entire state of the object
}

And

public UrlParamsHelper(Parcel in) {
    //I need this to unpack the state of the object
}

Here is the actual class

/*
  * 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];
       }
   };
 }

解决方案

In your writeToParcel function, you need to write which state objects you want to the Parcel, for instance:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(urlPath);
    dest.writeString(hostname);
}

It doesn't matter which order you write the objects, so long as you read them back in in the same order:

@Override
public UrlParamsHelper(Parcel in) {
    urlPath = in.readString();
    hostname = in.readString();
}

The problem is that you can only read and write the object types mentioned in the documentation for Parcel, so it may be difficult to save absolutely everything.