本文整理了Java中android.os.Parcel
类的一些代码示例,展示了Parcel
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parcel
类的具体详情如下:
包路径:android.os.Parcel
类名称:Parcel
[英]Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general Parcelable interface), and references to live IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
The bulk of the Parcel API revolves around reading and writing data of various types. There are six major classes of such functions available.
The most basic data functions are for writing and reading primitive data types: #writeByte, #readByte, #writeDouble, #readDouble, #writeFloat, #readFloat, #writeInt, #readInt, #writeLong, #readLong, #writeString, #readString. Most other data operations are built on top of these. The given data is written and read using the endianess of the host CPU.
There are a variety of methods for reading and writing raw arrays of primitive objects, which generally result in writing a 4-byte length followed by the primitive data items. The methods for reading can either read the data into an existing array, or create and return a new array. These available types are:
The Parcelable protocol provides an extremely efficient (but low-level) protocol for objects to write and read themselves from Parcels. You can use the direct methods #writeParcelable(Parcelable,int) and #readParcelable(ClassLoader) or #writeParcelableArray and #readParcelableArray(ClassLoader) to write or read. These methods write both the class type and its data to the Parcel, allowing that class to be reconstructed from the appropriate class loader when later reading.
There are also some methods that provide a more efficient way to work with Parcelables: #writeTypedArray, #writeTypedList(List), #readTypedArray and #readTypedList. These methods do not write the class information of the original object: instead, the caller of the read function must know what type to expect and pass in the appropriate Parcelable.Creator instead to properly construct the new object and read its data. (To more efficient write and read a single Parceable object, you can directly call Parcelable#writeToParcel and Parcelable.Creator#createFromParcel yourself.)
A special type-safe container, called Bundle, is available for key/value maps of heterogeneous values. This has many optimizations for improved performance when reading and writing data, and its type-safe API avoids difficult to debug type errors when finally marshalling the data contents into a Parcel. The methods to use are #writeBundle(Bundle), #readBundle(), and #readBundle(ClassLoader).
An unusual feature of Parcel is the ability to read and write active objects. For these objects the actual contents of the object is not written, rather a special token referencing the object is written. When reading the object back from the Parcel, you do not get a new instance of the object, but rather a handle that operates on the exact same object that was originally written. There are two forms of active objects available.
Binder objects are a core facility of Android's general cross-process communication system. The IBinder interface describes an abstract protocol with a Binder object. Any such interface can be written in to a Parcel, and upon reading you will receive either the original object implementing that interface or a special proxy implementation that communicates calls back to the original object. The methods to use are #writeStrongBinder(IBinder), #writeStrongInterface(IInterface), #readStrongBinder(), #writeBinderArray(IBinder[]), #readBinderArray(IBinder[]), #createBinderArray(), #writeBinderList(List), #readBinderList(List), #createBinderArrayList().
FileDescriptor objects, representing raw Linux file descriptor identifiers, can be written and ParcelFileDescriptor objects returned to operate on the original file descriptor. The returned file descriptor is a dup of the original file descriptor: the object and fd is different, but operating on the same underlying file stream, with the same position, etc. The methods to use are #writeFileDescriptor(FileDescriptor), #readFileDescriptor().
A final class of methods are for writing and reading standard Java containers of arbitrary types. These all revolve around the #writeValue(Object) and #readValue(ClassLoader)methods which define the types of objects allowed. The container methods are #writeArray(Object[]), #readArray(ClassLoader), #writeList(List), #readList(List,ClassLoader), #readArrayList(ClassLoader), #writeMap(Map), #readMap(Map,ClassLoader), #writeSparseArray(SparseArray), #readSparseArray(ClassLoader).
[中]可通过IBinder发送的消息(数据和对象引用)的容器。地块可以包含将在IPC另一侧取消扁平化的扁平化数据(使用此处用于写入特定类型的各种方法,或通用地块可传输接口),以及对活动IBinder对象的引用,这将导致另一侧接收与地块中原始IBinder连接的代理IBinder。
Parcel不是通用的序列化机制。此类(以及用于将任意对象放入包裹的相应Parcelable API)被设计为高性能IPC传输。因此,不适合将任何地块数据放入持久存储:地块中任何数据的底层实现中的更改都可能导致旧数据无法读取。
Parcel API的主要功能是读取和写入各种类型的数据。这类函数有六大类。
####原语
最基本的数据函数用于写入和读取基本数据类型:#writeByte、#readByte、#writeDouble、#readDouble、#writeFloat、#readFloat、#writeInt、#writeLong、#readLong、#writeString、#readString、#readString。大多数其他数据操作都建立在这些操作之上。使用主机CPU的endianess写入和读取给定的数据。
####基元数组
有多种方法可以读取和写入原始对象数组,这通常会导致写入一个4字节的长度,后跟原始数据项。读取方法可以将数据读入现有数组,也可以创建并返回新数组。这些可用类型包括:
*#WriteBoileAnarray(布尔[])、#readBooleanArray(布尔[])、#createBooleanArray()
*#writeByteArray(字节[]),#writeByteArray(字节[],int,int),#readByteArray(字节[]),#createByteArray()
*#writeCharArray(char[])、#readCharArray(char[])、#createCharArray()
*#writeDoubleArray(double[]),#readDoubleArray(double[]),#createDoubleArray()
*#writeFloatArray(float[])、#readFloatArray(float[])、#createFloatArray()
*#writeIntArray(int[])、#readIntArray(int[])、#createIntArray()
*#writeLongArray(long[])、#readLongArray(long[])、#createLongArray()
*#writeStringArray(字符串[])、#readStringArray(字符串[])、#createStringArray()。
*#writeSparseBooleanArray(SparseBooleanArray),#readSparseBooleanArray()。
####包裹
Parcelable协议为对象从包裹中写入和读取数据提供了一个非常高效(但级别较低)的协议。您可以使用直接方法#writeParcelable(Parcelable,int)和#readParcelable(ClassLoader)或#writeParcelableArray和#readParcelableArray(ClassLoader)进行写入或读取。这些方法将类类型及其数据写入包,允许在以后读取时从适当的类装入器重构该类。
还有一些方法提供了更有效的处理包裹的方法:#WriteyPedaray、#WriteyPedList(List)、#readTypedArray和#readTypedList。这些方法不会写入原始对象的类信息:相反,read函数的调用方必须知道预期的类型,并传入相应的Parcelable。而不是创建者来正确构造新对象并读取其数据。(要更高效地写入和读取单个可包裹对象,可以直接调用Parcelable#writeToParcel和Parcelable.Creator#createFromParcel。)
####捆
一个名为Bundle的特殊类型安全容器可用于异构值的键/值映射。在读写数据时,它进行了许多优化以提高性能,并且它的类型安全API避免了在最终将数据内容编组到包中时难以调试的类型错误。使用的方法有#writeBundle(Bundle)、#readBundle()和#readBundle(ClassLoader)。
####活动对象
Parcel的一个不同寻常的功能是能够读取和写入活动对象。对于这些对象,不会写入对象的实际内容,而是写入引用该对象的特殊标记。从地块读回对象时,不会得到该对象的新实例,而是会得到一个句柄,该句柄操作的对象与最初编写的对象完全相同。有两种形式的活动对象可用。
Binder对象是Android通用跨进程通信系统的核心功能。IBinder接口描述了一个带有Binder对象的抽象协议。任何这样的接口都可以写入到一个包中,读取后,您将收到实现该接口的原始对象或一个特殊的代理实现,该代理实现将调用传递回原始对象。使用的方法有#writeStrongBinder(IBinder),#writeStrongInterface(IInterface),#readStrongBinder(),#writeBinderArray(IBinder[]),#readBinderArray(IBinder[]),#createBinderArray(),#writeBinderList(List),#ReaderList(List),#CreateBinderArraList()。
可以写入FileDescriptor对象(表示原始Linux文件描述符标识符),并返回ParcelFileDescriptor对象以对原始文件描述符进行操作。返回的文件描述符是原始文件描述符的一个dup:对象和fd不同,但操作在相同的底层文件流上,位置相同,等等。使用的方法是#writeFileDescriptor(FileDescriptor),#readFileDescriptor()。
####非类型容器
最后一类方法用于编写和读取任意类型的标准Java容器。这些都围绕着#writeValue(Object)和#readValue(ClassLoader)方法展开,它们定义了允许的对象类型。容器方法有#writeArray(Object[])、#readArray(ClassLoader)、#writeList(List)、#readList(List、ClassLoader)、#readArrayList(ClassLoader)、#writeMap(Map)、#readMap(Map、ClassLoader)、#writesparserray(SparseArray)、#readSparseArray(ClassLoader)。
代码示例来源:origin: google/ExoPlayer
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(elementId);
dest.writeByte((byte) (isRoot ? 1 : 0));
dest.writeByte((byte) (isOrdered ? 1 : 0));
dest.writeStringArray(children);
dest.writeInt(subFrames.length);
for (Id3Frame subFrame : subFrames) {
dest.writeParcelable(subFrame, 0);
}
}
代码示例来源:origin: google/ExoPlayer
/* package */ ChapterTocFrame(Parcel in) {
super(ID);
this.elementId = castNonNull(in.readString());
this.isRoot = in.readByte() != 0;
this.isOrdered = in.readByte() != 0;
this.children = in.createStringArray();
int subFrameCount = in.readInt();
subFrames = new Id3Frame[subFrameCount];
for (int i = 0; i < subFrameCount; i++) {
subFrames[i] = in.readParcelable(Id3Frame.class.getClassLoader());
}
}
代码示例来源:origin: google/ExoPlayer
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(chapterId);
dest.writeInt(startTimeMs);
dest.writeInt(endTimeMs);
dest.writeLong(startOffset);
dest.writeLong(endOffset);
dest.writeInt(subFrames.length);
for (Id3Frame subFrame : subFrames) {
dest.writeParcelable(subFrame, 0);
}
}
代码示例来源:origin: airbnb/lottie-android
private SavedState(Parcel in) {
super(in);
animationName = in.readString();
progress = in.readFloat();
isAnimating = in.readInt() == 1;
imageAssetsFolder = in.readString();
repeatMode = in.readInt();
repeatCount = in.readInt();
}
代码示例来源:origin: airbnb/lottie-android
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeString(animationName);
out.writeFloat(progress);
out.writeInt(isAnimating ? 1 : 0);
out.writeString(imageAssetsFolder);
out.writeInt(repeatMode);
out.writeInt(repeatCount);
}
代码示例来源:origin: google/ExoPlayer
/* package */ ChapterFrame(Parcel in) {
super(ID);
this.chapterId = castNonNull(in.readString());
this.startTimeMs = in.readInt();
this.endTimeMs = in.readInt();
this.startOffset = in.readLong();
this.endOffset = in.readLong();
int subFrameCount = in.readInt();
subFrames = new Id3Frame[subFrameCount];
for (int i = 0; i < subFrameCount; i++) {
subFrames[i] = in.readParcelable(Id3Frame.class.getClassLoader());
}
}
代码示例来源:origin: android-hacker/VirtualXposed
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.packageName);
dest.writeString(this.apkPath);
dest.writeString(this.libPath);
dest.writeByte(this.dependSystem ? (byte) 1 : (byte) 0);
dest.writeInt(this.appId);
}
代码示例来源:origin: bumptech/glide
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(rowId);
parcel.writeString(uri.toString());
parcel.writeString(mimeType);
parcel.writeLong(dateTaken);
parcel.writeLong(dateModified);
parcel.writeInt(orientation);
parcel.writeString(type.name());
}
代码示例来源:origin: android-hacker/VirtualXposed
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.packageName);
dest.writeString(this.apkPath);
dest.writeString(this.libPath);
dest.writeByte(this.dependSystem ? (byte) 1 : (byte) 0);
dest.writeInt(this.appId);
//noinspection unchecked
dest.writeSparseArray((SparseArray) this.userState);
dest.writeByte(this.skipDexOpt ? (byte) 1 : (byte) 0);
}
代码示例来源:origin: bumptech/glide
private MediaStoreData(Parcel in) {
rowId = in.readLong();
uri = Uri.parse(in.readString());
mimeType = in.readString();
dateTaken = in.readLong();
dateModified = in.readLong();
orientation = in.readInt();
type = Type.valueOf(in.readString());
}
代码示例来源:origin: google/ExoPlayer
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mimeType);
dest.writeString(description);
dest.writeInt(pictureType);
dest.writeByteArray(pictureData);
}
代码示例来源:origin: android10/Android-CleanArchitecture
private SavedState(Parcel in) {
super(in);
this.imagePlaceHolderResId = in.readInt();
this.imageUrl = in.readString();
}
代码示例来源:origin: android10/Android-CleanArchitecture
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(this.imagePlaceHolderResId);
out.writeString(this.imageUrl);
}
代码示例来源:origin: google/ExoPlayer
private MdtaMetadataEntry(Parcel in) {
key = Util.castNonNull(in.readString());
value = new byte[in.readInt()];
in.readByteArray(value);
localeIndicator = in.readInt();
typeIndicator = in.readInt();
}
代码示例来源:origin: google/ExoPlayer
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(length);
for (int i = 0; i < length; i++) {
dest.writeParcelable(formats[i], 0);
}
}
代码示例来源:origin: google/ExoPlayer
/* package */ Metadata(Parcel in) {
entries = new Metadata.Entry[in.readInt()];
for (int i = 0; i < entries.length; i++) {
entries[i] = in.readParcelable(Entry.class.getClassLoader());
}
}
代码示例来源:origin: lingochamp/FileDownloader
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isLargeFile ? 1 : 0));
dest.writeByte(getStatus());
// normal
dest.writeInt(this.id);
}
代码示例来源:origin: google/ExoPlayer
/* package */ ApicFrame(Parcel in) {
super(ID);
mimeType = castNonNull(in.readString());
description = castNonNull(in.readString());
pictureType = in.readInt();
pictureData = castNonNull(in.createByteArray());
}
代码示例来源:origin: vondear/RxTool
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(facing);
out.writeParcelable(ratio, 0);
out.writeByte((byte) (autoFocus ? 1 : 0));
out.writeInt(flash);
}
代码示例来源:origin: vondear/RxTool
@SuppressWarnings("WrongConstant")
public SavedState(Parcel source, ClassLoader loader) {
super(source);
facing = source.readInt();
ratio = source.readParcelable(loader);
autoFocus = source.readByte() != 0;
flash = source.readInt();
}
内容来源于网络,如有侵权,请联系作者删除!