gpt4 book ai didi

android - 在 onRequestPermissionsResult 中,某些设备上的 grantResults 在用户拒绝权限时返回空

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:00 24 4
gpt4 key购买 nike

In Shot:,在 onRequestPermissionsResult 中,当用户拒绝权限时,某些设备上的 grantResults 返回空值,而某些设备具有值 PackageManager.PERMISSION_DENIED .

我已经实现了一个解决方案,用于识别用户选择了接受拒绝拒绝不再询问以获得运行时权限,基于 https://stackoverflow.com/a/31925748/2941375回答。

根据我看到的许多文档,如果用户拒绝许可,则返回 grantResults

我使用的代码 else if (grantResults[0] == PackageManager.PERMISSION_DENIED) 所以在 else if 部分它抛出 Arrayindexoutofbound 异常

i have tested code when user decline permission grantResults is not emplty for my case,but i have seen crash report on fabric console for grantResults there is many crash with 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]抛出异常。

最佳答案

根据 the documentation :

Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.

您希望如何处理取消完全取决于您(重新请求许可、将其视为拒绝等),因此请确保在您的代码中考虑到这种情况。

关于android - 在 onRequestPermissionsResult 中,某些设备上的 grantResults 在用户拒绝权限时返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50770955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com