检查用户是否已在Android

时间:2017-02-09 17:35:35

标签: android android-permissions

我已在String数组中定义了所有危险权限,如下所示:

String[] perms = {Manifest.permission.READ_CONTACTS,
                      Manifest.permission.READ_PHONE_STATE,
                      Manifest.permission.CALL_PHONE,
                      Manifest.permission.MODIFY_PHONE_STATE};

然后检查他们是否被授予我运行:

for (int i = 0; i < perms.length; i++) {
        if(ContextCompat.checkSelfPermission(this,perms[i])!=PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this,perms, permsRequestCode);
            break;
        }
    }

出于某种原因,这不起作用。它要求许可一次,然后如果我从设置中手动禁用它,它会多次调出对话框。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:15)

使用此选项可以检查您想要的任何权限,一次是单个权限还是多个权限。

"An unknown function with name 'utcnow' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed.\r\n     inner exception: An unknown function with name 'utcnow' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed."

}

答案 1 :(得分:11)

据我所知,这应该有效。但您可以尝试使用以下有效的功能。它的方法与你的方法不同。

String requiredPermission = "android.permission.READ_CONTACTS";
int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);

现在您可以查看:

if (checkVal==PackageManager.PERMISSION_GRANTED){}

答案 2 :(得分:0)

尝试在perms内添加String[]{}对我有用。

for (int i = 0; i < perms.length; i++) {
        if(ContextCompat.checkSelfPermission(this,perms[i])!=PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this,String[]{perms}, permsRequestCode);
            break;
        }
    }