繁体   English   中英

为什么ActivityCompat.requestPermissions()仅接受0-255之间的整数请求代码?

[英]Why does ActivityCompat.requestPermissions() only accept an integer request code between 0-255?

我最近将一个应用程序更新为targetSdkVersion为23,并实现了对各种权限的请求。 我最初使用ActivityCompat.requestPermissions()的尝试导致从FragmentActivity内部抛出IllegalArgumentException:

int REQUEST_CODE_A = 9001;
ActivityCompat.requestPermissions(new String[ {Manifest.permission.WRITE_CONTACTS}, REQUEST_CODE_A); // crashes

java.lang.IllegalArgumentException:只能将低8位用于requestCode

但是,如果请求代码在0-255之间,则一切正常,并且许可请求按预期方式工作。

int REQUEST_CODE_B = 101;
ActivityCompat.requestPermissions(new String[ {Manifest.permission.WRITE_CONTACTS}, REQUEST_CODE_B); // works correctly

所以问题是,为什么在这样的API中限制整数的可能值是什么原因? 可以使用字节来提供相同的信息,但是(显然)已经做出有意识的决定,而不是使用整数。 这仅仅是允许将来扩展的情况吗?

将源代码引用FragmentActivity ,在此似乎抛出此异常:

    // We use 8 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.

startActivityForResult()至少在FragmentActivity对请求代码有16位限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM