Android:为 requestcode 值选择啥?
Posted
技术标签:
【中文标题】Android:为 requestcode 值选择啥?【英文标题】:Android: what to choose for requestcode values?Android:为 requestcode 值选择什么? 【发布时间】:2016-01-24 16:28:00 【问题描述】:ActivityCompat.requestPermissions
之类的方法要求我向它们传递一个请求代码,以便稍后在回调中进行测试(在本例中为 onRequestPermissionsResult
)。我应该在请求代码中传递一些最佳实践类型的值吗?我注意到,如果我只是输入一个随机的int
,我有时会收到这样的错误:
java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode
10-25 16:47:43.652 8315-8315/? E/androidRuntime: FATAL EXCEPTION: main
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: Process: my package, PID: 8315
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfomypackage.myactivity: java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.support.v4.app.FragmentActivity.validateRequestPermissionsRequestCode(FragmentActivity.java:799)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompat23.java:29)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:316)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at mypackage.myactivity.checkReadPhoneState(PermissionsGatewayActivity.java:48)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at mypackage.myactivity.onCreate(PermissionsGatewayActivity.java:36)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
【问题讨论】:
尝试 1 到 255 之间的请求代码。 这是什么意思?所以理论上你永远不会有超过 256 个并行运行的请求? (虽然那会很荒谬) 我认为这个数字在现实世界中已经足够了。 我们需要调用 ActivityCompat.requestPermissions() 方法的权限少于 30 个(危险权限)。所以我认为256绰绰有余。不是吗? 我觉得让我生气的是请求代码参数是一个 int ,此外,文档没有提到 8 位限制。 FFS 【参考方案1】:记录调查结果以供将来参考:
以下代码来自android.support.v4.app.FragmentActivity
/**
* Modifies the standard behavior to allow results to be delivered to fragments.
* This imposes a restriction that requestCode be <= 0xffff.
*/
@Override
public void startActivityForResult(Intent intent, int requestCode)
if (requestCode != -1 && (requestCode&0xffff0000) != 0)
throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
super.startActivityForResult(intent, requestCode);
@Override
public final void validateRequestPermissionsRequestCode(int requestCode)
// We use 16 bits of the request code to encode the fragment id when
// requesting permissions from a fragment. Hence, requestPermissions()
// should validate the code against that but we cannot override it as
// we can not then call super and also the ActivityCompat would call
// back to this override. To handle this we use dependency inversion
// where we are the validator of request codes when requesting
// permissions in ActivityCompat.
if (!mRequestedPermissionsFromFragment
&& requestCode != -1 && (requestCode & 0xffff0000) != 0)
throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
RANGEFragmentActivity
中的startActivityForResult()
要求 requestCode 为 16 位,这意味着范围是从 0 到 65535。
另外,FragmentActivity
中的validateRequestPermissionsRequestCode
要求 requestCode 为 16 位,这意味着范围是从 0 到 65535。
【讨论】:
范围已更改。 android.googlesource.com/platform/frameworks/support/+/… @Toris 谢谢。更新了我的答案。 ?仅在 Android 上。也许我们可以/应该在 AOSP 代码 cmets 中放置此答案的链接,以便在 AOSP 中更新此代码解析逻辑的任何人都记得在此处发布更新...以上是关于Android:为 requestcode 值选择啥?的主要内容,如果未能解决你的问题,请参考以下文章
Android:requestCode 和 resultCode
如何在android报警时从待处理的意图中获取requestCode