web-dev-qa-db-ja.com

Android:リクエストコードの値には何を選択すればよいですか?

ActivityCompat.requestPermissionsなどのメソッドでは、後でコールバックでテストできるリクエストコード(この場合はonRequestPermissionsResult)を渡す必要があります。 requestcodeで渡すことになっている値のベストプラクティスの種類はありますか?ランダムな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) 
38
Jon

今後の参考のために調査結果を文書化する:

以下は_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 FragmentActivityでは、requestCodeが16ビットである必要があります。これは、範囲が〜65535であることを意味します。

また、validateRequestPermissionsRequestCodeFragmentActivityでは、requestCodeが16ビットである必要があります。つまり、範囲は〜65535になります。

84
Henry