У меня есть класс (ниже), который я хочу отправить классу обслуживания через намерение. Я реализовал интерфейс 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];
}
};
}