android.os.Parcelable类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(113)

本文整理了Java中android.os.Parcelable类的一些代码示例,展示了Parcelable类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parcelable类的具体详情如下:
包路径:android.os.Parcelable
类名称:Parcelable

Parcelable介绍

[英]Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.

A typical implementation of Parcelable is:

public class MyParcelable implements Parcelable { 
private int mData; 
public int describeContents() { 
return 0; 
} 
public void writeToParcel(Parcel out, int flags) { 
out.writeInt(mData); 
} 
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { 
public MyParcelable createFromParcel(Parcel in) { 
return new MyParcelable(in); 
} 
public MyParcelable[] newArray(int size) { 
return new MyParcelable[size]; 
} 
}; 
private MyParcelable(Parcel in) { 
mData = in.readInt(); 
} 
}

[中]接口,用于实例可以写入地块并从地块恢复的类。实现Parcelable接口的类还必须具有名为CREATOR的静态字段,该字段是实现Parcelable的对象。创建者界面。
Parcelable的一个典型实现是:

public class MyParcelable implements Parcelable { 
private int mData; 
public int describeContents() { 
return 0; 
} 
public void writeToParcel(Parcel out, int flags) { 
out.writeInt(mData); 
} 
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { 
public MyParcelable createFromParcel(Parcel in) { 
return new MyParcelable(in); 
} 
public MyParcelable[] newArray(int size) { 
return new MyParcelable[size]; 
} 
}; 
private MyParcelable(Parcel in) { 
mData = in.readInt(); 
} 
}

代码示例

代码示例来源:origin: stackoverflow.com

public class ParcelableUtil {    
  public static byte[] marshall(Parcelable parceable) {
    Parcel parcel = Parcel.obtain();
    parceable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
  }

  public static Parcel unmarshall(byte[] bytes) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0); // This is extremely important!
    return parcel;
  }

  public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
    Parcel parcel = unmarshall(bytes);
    T result = creator.createFromParcel(parcel);
    parcel.recycle();
    return result;
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

@Override
public int describeContents() {
  int contents = 0;
  for (int i = 0; i < mList.size(); i++) {
    contents |= mList.get(i).describeContents();
  }
  return contents;
}

代码示例来源:origin: commonsguy/cw-omnibus

public static byte[] toByteArray(Parcelable parcelable) {
 Parcel parcel=Parcel.obtain();
 parcelable.writeToParcel(parcel, 0);
 byte[] result=parcel.marshall();
 parcel.recycle();
 return(result);
}

代码示例来源:origin: kingargyle/adt-leanback-support

@Override
public int describeContents() {
  return mInner.describeContents();
}

代码示例来源:origin: stackoverflow.com

p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

代码示例来源:origin: bzsome/VirtualApp-x326

@Override
public int describeContents() {
  int contents = 0;
  for (int i = 0; i < mList.size(); i++) {
    contents |= mList.get(i).describeContents();
  }
  return contents;
}

代码示例来源:origin: k9mail/k-9

public static byte[] marshall(Parcelable parceable) {
  Parcel parcel = Parcel.obtain();
  parceable.writeToParcel(parcel, 0);
  byte[] bytes = parcel.marshall();
  parcel.recycle();
  return bytes;
}

代码示例来源:origin: darkskygit/VirtualApp

@Override
public int describeContents() {
  int contents = 0;
  for (int i = 0; i < mList.size(); i++) {
    contents |= mList.get(i).describeContents();
  }
  return contents;
}

代码示例来源:origin: android-hacker/VirtualXposed

private static ScanResult cloneScanResult(Parcelable scanResult) {
  Parcel p = Parcel.obtain();
  scanResult.writeToParcel(p, 0);
  p.setDataPosition(0);
  ScanResult newScanResult = Reflect.on(scanResult).field("CREATOR").call("createFromParcel", p).get();
  p.recycle();
  return newScanResult;
}

代码示例来源:origin: stackoverflow.com

parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
dataMap.putByteArray(key, parcel.marshall());

代码示例来源:origin: stackoverflow.com

public static byte[] pack(Parcelable parcelable) {
  Parcel parcel = Parcel.obtain();
  parcelable.writeToParcel(parcel, 0);
  byte[] bytes = parcel.marshall();
  parcel.recycle();
  return bytes;
}

public static <T> T unpack(byte[] bytes, Parcelable.Creator<T> creator) {
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(bytes, 0, bytes.length);
  parcel.setDataPosition(0);
  return creator.createFromParcel(parcel);
}

MyObject myObject = unpack(new byte[]{/* bytes */}, MyObject.CREATOR);

代码示例来源:origin: codezjx/AndLinker

@Override
public void writeToParcel(Parcel dest, int flags, Parcelable val) {
  if (flags == Parcelable.PARCELABLE_WRITE_RETURN_VALUE) {
    val.writeToParcel(dest, flags);
  } else {
    dest.writeParcelable(val, flags);
  }
}

代码示例来源:origin: klinker24/Android-Blur-Launcher

@Override
public void writeToParcel(Parcel dest, int flags) {
  ContentValues itemValues = new ContentValues();
  writeToValues(itemValues);
  itemValues.writeToParcel(dest, flags);
  dest.writeInt(mArg1);
  dest.writeInt(mObjectType);
  if (mObject != null) {
    dest.writeInt(1);
    mObject.writeToParcel(dest, flags);
  } else {
    dest.writeInt(0);
  }
}

代码示例来源:origin: klinker24/launcher3

@Override
public void writeToParcel(Parcel dest, int flags) {
  ContentValues itemValues = new ContentValues();
  writeToValues(itemValues);
  itemValues.writeToParcel(dest, flags);
  dest.writeInt(mArg1);
  dest.writeInt(mObjectType);
  if (mObject != null) {
    dest.writeInt(1);
    mObject.writeToParcel(dest, flags);
  } else {
    dest.writeInt(0);
  }
}

代码示例来源:origin: braintree/braintree_android

public static void putParcelable(Context context, String key, Parcelable parcelable) {
  Parcel parcel = Parcel.obtain();
  parcelable.writeToParcel(parcel, 0);
  getSharedPreferences(context).edit()
      .putString(key, Base64.encodeToString(parcel.marshall(), 0))
      .apply();
}

代码示例来源:origin: cn.leancloud.android/avoscloud-statistics

private static byte[] marshall(Parcelable parcelable) {
 Parcel outer = Parcel.obtain();
 parcelable.writeToParcel(outer, 0);
 byte[] data = outer.marshall();
 return data;
}

代码示例来源:origin: BoD/jraf-android-util

/**
 * Parcel a parcelable to a byte array.
 *
 * @param parcelable The object to parcel.
 * @return A byte array representing the given object, or {@code null} if {@code parcelable} was {@code null}.
 */
@Nullable
public static byte[] parcel(@Nullable Parcelable parcelable) {
  if (parcelable == null) return null;
  Parcel parcel = Parcel.obtain();
  parcelable.writeToParcel(parcel, 0);
  byte[] bytes = parcel.marshall();
  parcel.recycle();
  return bytes;
}

代码示例来源:origin: tajchert/BusWear

/**
 * Converts the Parcelable object to a byte[]
 *
 * @param parcelable
 * @return
 */
public static byte[] parcelToByte(@NonNull Parcelable parcelable) {
  Parcel parcel = Parcel.obtain();
  parcelable.writeToParcel(parcel, 0);
  byte[] bytes = parcel.marshall();
  parcel.recycle();
  return bytes;
}

代码示例来源:origin: stackoverflow.com

p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

代码示例来源:origin: stackoverflow.com

p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

相关文章