下面列出了怎么用android.os.Parcel的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void readFromParcel(Parcel source) {
@SuppressWarnings("unchecked")
ArrayList<ReadPostsTask.UserPostPending> userPostPendings = source
.readArrayList(PostsExtra.class.getClassLoader());
if (userPostPendings.size() > 0) {
this.userPostPendings.addAll(userPostPendings);
}
String[] data = source.createStringArray();
if (data != null) {
Collections.addAll(expandedPosts, data);
}
isAddedToHistory = source.readInt() != 0;
forceRefresh = source.readInt() != 0;
newPostNumber = source.readString();
}
public boolean newSession(ICustomTabsCallback callback) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
boolean _result;
try {
_data.writeInterfaceToken("android.support.customtabs.ICustomTabsService");
_data.writeStrongBinder(callback != null?callback.asBinder():null);
this.mRemote.transact(3, _data, _reply, 0);
_reply.readException();
_result = 0 != _reply.readInt();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Test
public void parcelsCorrectly() {
PayPalLineItem item = new PayPalLineItem(PayPalLineItem.KIND_DEBIT, "An Item", "1", "2");
item.setDescription("A new item");
item.setProductCode("abc-123");
item.setUnitTaxAmount("1.50");
item.setUrl("http://example.com");
Parcel parcel = Parcel.obtain();
item.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
PayPalLineItem result = PayPalLineItem.CREATOR.createFromParcel(parcel);
assertEquals("debit", result.getKind());
assertEquals("An Item", result.getName());
assertEquals("1", result.getQuantity());
assertEquals("2", result.getUnitAmount());
assertEquals("A new item", result.getDescription());
assertEquals("abc-123", result.getProductCode());
assertEquals("1.50", result.getUnitTaxAmount());
assertEquals("http://example.com", result.getUrl());
}
@Test
public void parcelsCorrectly_ifCreditFinancingNotPresent() throws JSONException {
JSONObject response = new JSONObject(stringFromFixture("payment_methods/paypal_account_response.json"));
response.getJSONArray("paypalAccounts").getJSONObject(0).getJSONObject("details").remove("creditFinancingOffered");
PayPalAccountNonce payPalAccountNonce = PayPalAccountNonce.fromJson(response.toString());
assertNull(payPalAccountNonce.getCreditFinancing());
Parcel parcel = Parcel.obtain();
payPalAccountNonce.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
PayPalAccountNonce parceled = PayPalAccountNonce.CREATOR.createFromParcel(parcel);
assertNull(parceled.getCreditFinancing());
assertEquals("with email [email protected]", parceled.getDescription());
assertEquals("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", parceled.getNonce());
assertEquals("[email protected]", parceled.getEmail());
assertEquals("PayPal", parceled.getTypeLabel());
assertEquals("123 Fake St.", parceled.getBillingAddress().getStreetAddress());
assertEquals("Apt. 3", parceled.getBillingAddress().getExtendedAddress());
assertEquals("Oakland", parceled.getBillingAddress().getLocality());
assertEquals("CA", parceled.getBillingAddress().getRegion());
assertEquals("94602", parceled.getBillingAddress().getPostalCode());
assertEquals("US", parceled.getBillingAddress().getCountryCodeAlpha2());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(deviceId);
dest.writeString(appVersion);
dest.writeLong(timestamp);
dest.writeString(osVersion);
dest.writeString(ipConnectivity);
dest.writeString(dnResolvability);
dest.writeParcelable(location, flags);
dest.writeString(locationType);
dest.writeString(networkType);
dest.writeString(carrier);
dest.writeString(countryCode);
dest.writeInt(batteryLevel);
dest.writeByte((byte) (isBatteryCharging ? 1 : 0));
dest.writeString(cellInfo);
dest.writeString(cellRssi);
dest.writeInt(rssi);
dest.writeString(ssid);
dest.writeString(bssid);
dest.writeString(wifiIpAddress);
dest.writeString(mobilyzerVersion);
dest.writeStringList(hostApps);
dest.writeString(requestApp);
}
@Override
public void writeToParcel(Parcel out, int flags) {
if (mRelroFd >= 0) {
out.writeLong(mLoadAddress);
out.writeLong(mLoadSize);
out.writeLong(mRelroStart);
out.writeLong(mRelroSize);
try {
ParcelFileDescriptor fd = ParcelFileDescriptor.fromFd(mRelroFd);
fd.writeToParcel(out, 0);
fd.close();
} catch (java.io.IOException e) {
Log.e(TAG, "Cant' write LibInfo file descriptor to parcel", e);
}
}
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (circleEditing ? 1 : 0));
dest.writeInt(size.x);
dest.writeInt(size.y);
dest.writeParcelable(editorElementHierarchy.getRoot(), flags);
dest.writeParcelable(undoRedoStacks, flags);
dest.writeParcelable(cropUndoRedoStacks, flags);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(total_number);
dest.writeString(previous_cursor);
dest.writeString(next_cursor);
dest.writeTypedList(favorites);
dest.writeTypedList(actualStore);
}
@SuppressWarnings("CastToConcreteClass")
public CommandResult(Parcel inParcel) {
this(inParcel.readLong(),
inParcel.readInt(),
inParcel.readString(),
inParcel.readString(),
inParcel.readLong());
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(mId);
parcel.writeString(mTitle);
parcel.writeString(description);
parcel.writeString(mImageUrl);
parcel.writeInt(type);
if (parent == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
((DevCatalog) parent).writeToParcel(parcel, i);
}
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(caption);
dest.writeString(description);
dest.writeParcelable(fb_checkin, PARCELABLE_WRITE_RETURN_VALUE);
dest.writeString(fb_object_id);
dest.writeString(fb_object_type);
dest.writeString(href);
dest.writeString(icon);
dest.writeTypedList(media);
dest.writeString(name);
dest.writeString(properties);
}
private Voice(Parcel in) {
this.mName = in.readString();
this.mLocale = (Locale)in.readSerializable();
this.mQuality = in.readInt();
this.mLatency = in.readInt();
this.mRequiresNetworkConnection = (in.readByte() == 1);
this.mFeatures = new HashSet<String>();
Collections.addAll(this.mFeatures, in.readStringArray());
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
super.writeToParcel(dest, flags);
dest.writeString(value);
dest.writeString(defaultValue);
}
protected MissServiceBean(Parcel in) {
timeLine = in.createTypedArrayList(RecordViewModel.CREATOR);
contacts = in.createTypedArrayList(ContactViewModel.CREATOR);
newest = new FixedList<NewestModel>(5);
List<NewestModel> models = in.createTypedArrayList(NewestModel.CREATOR);
newest.addAll(models);
birthdayCount = in.readLong();
memorialCount = in.readLong();
futureCount = in.readLong();
}
public static byte[] marshall(Parcelable parcelable) {
Parcel parcel = Parcel.obtain();
parcelable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(leaveApp);
dest.writeString(confirm);
dest.writeString(cancel);
dest.writeString(title);
}
public Botification createFromParcel(Parcel in) {
int id = in.readInt();
String pkg = in.readString();
String tag = in.readString();
String description = in.readString();
String text = in.readString();
return new Botification(id, pkg, tag, description, text);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(event);
dest.writeString(created);
dest.writeString(userId);
dest.writeByte((byte) (enabledTelemetry ? 0x01 : 0x00));
dest.writeString(device);
dest.writeString(sdkIdentifier);
dest.writeString(sdkVersion);
dest.writeString(model);
dest.writeString(operatingSystem);
dest.writeString(skuId);
}
private void readFromParcel(Parcel source) throws JSONException {
date = source.readString();
currentCity = source.readString();
pm25 = source.readString();
index = new JSONArray(source.readString());
weather_data = new JSONObject(source.readString());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mId);
dest.writeInt(mUniqueId);
dest.writeInt(mName);
dest.writeInt(mDescription);
dest.writeString(mNameText);
dest.writeString(mDescriptionText);
dest.writeString(mDefault);
dest.writeByte((byte) (mRequired ? 1 : 0));
dest.writeByte((byte) (mScript ? 1 : 0));
dest.writeInt(mUnit == null ? -1 : mUnit.getId());
}
@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.url);
dest.writeString(this.fileName);
dest.writeByte(this.isGroupChild ? (byte) 1 : (byte) 0);
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
dest.writeString(this.redirectUrl);
}
@Override
public MessagePdu createFromParcel(Parcel source) {
int size = source.readInt();
List<byte[]> pduList;
if (size == NULL_LENGTH) {
pduList = null;
} else {
pduList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
pduList.add(source.createByteArray());
}
}
return new MessagePdu(pduList);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(mSerializedPattern);
dest.writeInt(mDisplayMode);
dest.writeValue(mInputEnabled);
dest.writeValue(mInStealthMode);
dest.writeValue(mTactileFeedbackEnabled);
}
private FileLocation(Parcel in) {
this.title = in.readString();
this.file = in.readString();
this.isDirectory = (in.readInt() != 0);
this.hasParent = (in.readInt() != 0);
this.isRoot = (in.readInt() != 0);
this.details = in.readString();
this.sizeDuration = in.readString();
this.artUrl = in.readString();
}
public static void writeStringMap(Parcel parcel, Map<String, String> stringMap) {
Bundle b = new Bundle();
for (Map.Entry<String, String> entry : stringMap.entrySet()) {
b.putString(entry.getKey(), entry.getValue());
}
parcel.writeBundle(b);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isLargeFile ? 1 : 0));
dest.writeByte(getStatus());
// normal
dest.writeInt(this.id);
}
@Override
public Transaction createFromParcel(Parcel in) {
return new Transaction(in);
}
@Override
public ERC721Token createFromParcel(Parcel in) {
return new ERC721Token(in);
}
@Override
public SmsMarker createFromParcel(Parcel source) {
return new SmsMarker(source);
}
public RequestActivityForResult createFromParcel(Parcel in) {
return new RequestActivityForResult(in);
}