In Shot:, In onRequestPermissionsResult
某些设备上的 grantResults 返回空,而某些设备上有一个值PackageManager.PERMISSION_DENIED
当用户拒绝许可时。
我已经实现了一个解决方案来识别用户已选择的accepted ,deny and 否认并不再询问对于运行时权限,基于https://stackoverflow.com/a/31925748/2941375 https://stackoverflow.com/a/31925748/2941375 answer.
根据我见过的许多文档,如果用户拒绝许可,那么它会返回grantResults
empty
我用过的代码else if (grantResults[0] == PackageManager.PERMISSION_DENIED)
so in else if
部分它抛出 Arrayindexoutofbound 异常
当用户拒绝许可时我测试了代码grantResults
对于我的情况来说不为空,但我已经在结构控制台上看到了崩溃报告grantResults
有很多崩溃arrayindexoutofbound
,
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PermissionManager.MY_PERMISSIONS_REQUEST_LOCATION_ACCESS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
DefineLocationService.start(this);
startNextActivity(0);
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0]);
if (!showRationale) {
// user also CHECKED "never ask again"
// you can either enable some fall back,
// disable features of your app
// or open another dialog explaining
// again the permission and directing to
// the app setting
startNextActivity(ARTIFICIAL_DELAY_MILLIS);
} else if (!PermissionManager.MY_REQUESTED_DIALOG) {
PermissionManager.checkLocationPermission(this);
} else {
startNextActivity(0);
}
} else {
startNextActivity(ARTIFICIAL_DELAY_MILLIS);
}
}
}
}
谁能对此有任何解释,为什么有些设备会返回grantResults
空且某些设备返回grantResults
当用户拒绝许可时具有下降的价值。
我已经测试了很多次但是grantResults
我这边从来没有空过,但是控制台上有崩溃,这意味着在某些情况下它是空的并且grantResults[0]
抛出异常。