其他分享
首页 > 其他分享> > android-在onRequestPermissionsResult GrantResults在某些设备上,当用户拒绝权限时返回空

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

作者:互联网

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

我采用了一种解决方案,用于基于https://stackoverflow.com/a/31925748/2941375答案来识别用户已选择接受,拒绝和拒绝,而无需再次询问运行时权限.

根据我看到的许多文档,如果用户拒绝权限,则它会返回GrantResults为空

我使用其他代码if(grantResults [0] == PackageManager.PERMISSION_DENIED)在其他代码中抛出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.

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

标签:permission-denied,android-permissions,runtime-permissions,android
来源: https://codeday.me/bug/20191025/1926215.html