Parcelable中标志的用途是什么? [英] What is the use of flags in Parcelable?

查看:96
本文介绍了Parcelable中标志的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将Parcelable写入Parcel,而没有将重点放在标志字段上,这是方法签名中的一个参数,它工作正常,但是我遇到了一个无法再忽略它们的实现:

I have been writing Parcelables to Parcel without any focus on flags field, which is a parameter in the method signature and it has worked fine but I have run into an implementation where I can no longer ignore them:

public static <K extends Parcelable, V extends Parcelable> void write(Parcel dest,
                                                    Map<K, V> map, int flags) {
        if (map == null) {
            dest.writeInt(-1);
        } else {
            Set<Map.Entry<K, V>> entrySet = map.entrySet();
            dest.writeInt(entrySet.size());
            for (Map.Entry<K, V> entry : entrySet) {
                dest.writeParcelable(entry.getKey(), flags);
                dest.writeParcelable(entry.getValue(), flags);
            }
        }
    }

这是我编写的Map 进/出 Parcelable实用程序,我想知道是否应该将标志按原样传递给两个 Key 以及编写它们时的 Value ,或者对于 Key 应当传递0,对于 Value 应当传递flags.

This is a Map to/from Parcelable utility I have written and I am wondering if the flags should be passed as it is to both Key as well as the Value while writing them or should pass 0 for Key and flags for Value.

我阅读了文档中标志的定义a>:

I read the definition of what a flag is in the docs:

PARCELABLE_WRITE_RETURN_VALUE

已添加到API级别1

int PARCELABLE_WRITE_RETURN_VALUE

writeToParcel(Parcel, int)一起使用的标记: 写入的是一个返回值,它是诸如以下函数的结果 "Parcelable someFunction()" 没有someFunction(不可包裹的包裹)" 没有someFunction(不可包裹的包裹)" .一些 实现可能会在此时释放资源.

Flag for use with writeToParcel(Parcel, int): the object being written is a return value, that is the result of a function such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)". Some implementations may want to release resources at this point.

常量值:1(0x00000001)

Constant Value: 1 (0x00000001)

但是我无法理解.谁能简单地解释Parcelable标志是什么以及应如何使用?

But am unable to comprehend it. Could anyone explain in simple terms what a Parcelable flag is and how it should be used?

推荐答案

当前唯一存在的标志(PARCELABLE_WRITE_RETURN_VALUE)用于

The only currently existing flag (PARCELABLE_WRITE_RETURN_VALUE) is intended for use in AIDL interfaces. It is supposed to hint certain kinds of Parcelable objects, that they are being returned from IPC method, so their associated resources can be released. Fot instance, ContentProvider internally contains AIDL method like this:

ParcelFileDescriptor openFile(String path, int flags);

当您在自定义ContentProvider中覆盖openFile时,您的方法将返回 open ParcelFileDescriptor…您自己并没有关闭它,并且在进程间传输期间它也不会自动关闭(在进程并不意味着在Linux中将其关闭).但是描述符没有泄漏!相反,ParcelFileDescriptor 会自行关闭写入包裹时:

When you override openFile in a custom ContentProvider, your method returns an open ParcelFileDescriptor… You aren't closing it yourself, and it is not automatically closed during interprocess transfer either (passing descriptors between processes does not imply closing them in Linux). But the descriptor is not leaked! Instead the ParcelFileDescriptor closes itself when written to Parcel:

@Override
public void writeToParcel(Parcel out, int flags) {
    if (mWrapped != null) {
        try {
            mWrapped.writeToParcel(out, flags);
        } finally {
            releaseResources();
        }
    } else {
        if (mCommFd != null) {
            out.writeInt(1);
            out.writeFileDescriptor(mFd);
            out.writeFileDescriptor(mCommFd);
        } else {
            out.writeInt(0);
            out.writeFileDescriptor(mFd);
        }
        if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0 && !mClosed) {
            // Not a real close, so emit no status
            closeWithStatus(Status.SILENCE, null);
        }
    }
}

由于ParcelFileDescriptor只是普通类,使用Binder/Parcel的功能在进程之间传递FileDescriptor,因此您可以想象存在类似的类,这些类保留本地资源(内存,文件描述符),并在从openFile返回时有条件地释放它们.相似的方法.

Since ParcelFileDescriptor is just ordinary class, using facilities of Binder/Parcel to pass FileDescriptor between processes, you can imagine existence of similar classes, that hold onto native resources (memory, file descriptors) and conditionally release them when returned from openFile-like methods.

同样,可以使用其他标志 在Parcelable matryoshka的深处传播类似的条件行为.不幸的是,Android开发人员尚未为引入此类自定义标志定义合理的规则(与IBinder#FIRST_CALL_TRANSACTIONIBinder#LAST_CALL_TRANSACTION不同),并且AIDL并未在Android内部内部广泛使用,因此我不知道此类示例标志.

Likewise, other flags could be used to propagate similar conditional behavior deeply down Parcelable matryoshka. Unfortunately, Android developers haven't defined reasonable rules for introducing such custom flags (unlike e.g. IBinder#FIRST_CALL_TRANSACTION and IBinder#LAST_CALL_TRANSACTION), and AIDL is not widely used in practice outside of Android internals, so I am not aware of any examples of such flags.

这篇关于Parcelable中标志的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆