Java Android:为请求代码值选择什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33331073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:34:45  来源:igfitidea点击:

Android: what to choose for requestcode values?

javaandroiduser-interfaceandroid-permissionsandroid-6.0-marshmallow

提问by Jon

Methods like ActivityCompat.requestPermissionsrequire that I pass them a requestcode that I can later test in a callback (in this case onRequestPermissionsResult). Is there some best practice sort of value I'm supposed to pass in the requestcode? I've noticed that if I just enter a random intI sometimes get an error like this:

像这样的方法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 ComponentInfo{mypackage.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)?

采纳答案by Henry

Documenting the findings for future reference:

记录调查结果以备将来参考:

The following are code from android.support.v4.app.FragmentActivity

以下代码来自 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");
    }
}


RANGE
startActivityForResult()in FragmentActivityrequires the requestCode to be of 16 bits, meaning the range is from 0 to 65535.


RANGE
startActivityForResult()inFragmentActivity要求 requestCode 为 16 位,这意味着范围是从0 到 65535

Also, validateRequestPermissionsRequestCodein FragmentActivityrequires requestCode to be of 16 bits, meaning the range is from 0 to 65535.

此外,validateRequestPermissionsRequestCodeinFragmentActivity要求 requestCode 为 16 位,这意味着范围是从0 到 65535