在 Android 中为 startActivityForResult() 生成 16 位唯一 ID [英] Generate 16 bit unique IDs in Android for startActivityForResult()

查看:50
本文介绍了在 Android 中为 startActivityForResult() 生成 16 位唯一 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将生成的资源 ID 用于我所有的 startActivityForResult() 代码,这样我就可以在基类中使用 onActivityResult() 而不必担心派生类使用相同的代码.

I was planning to use generated resource IDs for all my startActivityForResult() codes, so that I can use onActivityResult() in base classes and not have to worry if the derived class used the same code.

不幸的是,代码似乎限制为 16 位,而资源 ID 为 32 位.有没有办法生成唯一的 16 位 ID?

Unfortunately it seems that the codes are restricted to 16 bits, and resource IDs are 32 bits. Is there a way to generate unique 16 bit IDs instead?

推荐答案

其实是有的.此外,您可以使用标准 ID 作为 Android 资源.只需用 0x0000FFFF 屏蔽您的 id 并在您想要的任何地方使用它作为 startActivityForResult()requestPermissions() 的 ID,您可以使用这个简单的实用程序:

Actually there is. Moreover you can use standard id as Android resource. Simply just mask your id with 0x0000FFFF and use it wherever you want as ID for startActivityForResult() or requestPermissions() , you may use this simple utility:

public static int normalizeId(int id){
         return id & 0x0000FFFF;
}

为什么?首先,让我们指出限制 16 位值背后的原因.这是片段/活动.操作系统强制开发人员使用 16 位,而 ID 是 32 位(作为整数),因为系统总是用 0xffff 掩码 id,然后在调用来自 Fragment 时将其移位为 16(<<16).所以它是一个唯一的 id 标记为片段目标 Id.另一方面,通过活动发送的 id 保持原样,所以它的活动目标 Id.然后当结果出来时,操作系统知道发送到 Fragment 或 Activity 的位置.假设我们有这个 id Id=0x0001

Why? Firstly, Lets point to the reason behind that limitation to 16 bit vlaue. It's Fragment/Activity. OS enforces developers to use 16 bit while ID is 32 bit(as integer number) because system always masks id with 0xffff then shifts it with 16 (<<16) when call comes from Fragment. so it's a unique id marked as fragment target Id.On the other side, the id sent via activity stays as it's, so its activity target Id. Then when results come out, OS knows where to send whether to Fragment or Activity. Lets say we have this id Id=0x0001

Id in startActivityForResult() in Activity 变为(无变化):

Id in startActivityForResult() in Activity becomes(no-change):

Id=0x0001

Id in startActivityForResult() in Fragment 变成:

Id in startActivityForResult() in Fragment becomes:

Id=0xFFFF0001

现在我们怎么可以忽略前 16 位?让我们来看看 Android 中任何资源的 id 剖析.它由三部分组成,作为 HEX 值构建:

Now how comes we can just ignore the first 16 bit ? lets take a look on anatomy of id of any resource in Android. it composes of three parts as HEX value construction:

PPTTVVVV

PP:表示包 ID.其中有两个:

PP: represents package id. There are two of them:

  • 0x01(系统包 ID)
  • 0x7f(应用包 ID)

TT:代表类型 ID.即:

TT: represents type Id. i.e.:

  • 0x0b : 数组
  • 0x01:属性
  • 0x0c : 尺寸
  • 0x10:颜色
  • 0x0e:布尔
  • 0x02:可绘制
  • .. 等

VVVV:代表特定包ID下特定类型资源的真实唯一标识.

VVVV: represents real unique identification of specific type of resource under specific package id.

正如您现在看到的,忽略代表 PPTT 的前 16 位不会对您的应用产生影响或导致与 id 冲突.所以安全地只使用 VVVV 部分,它是 startActivityForResult()requestPermissions()

As you can see now that ignoring first 16bit which represents PPTT will not have impact on your app or leading to conflict with ids. So safely use only VVVV part which is 16 bit value in startActivityForResult() or requestPermissions()

希望能帮到你,'.

这篇关于在 Android 中为 startActivityForResult() 生成 16 位唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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