ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-25 05:37:04  阅读:1201  来源: 互联网

标签:permission-denied android-permissions runtime-permissions android


在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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有