向android请求两个不同的权限

时间:2018-05-07 19:13:17

标签: android permissions

我需要请求发送短信和阅读联系人的许可。

我设法让android请求使用以下代码发送短信的权限:

        if(ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.SEND_SMS)){
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.SEND_SMS},1);
        } else {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.SEND_SMS},1);
        }
    } else {
        // do nothing
    }

我尝试使用READ_CONTACT而不是SEND_SMS添加上面的代码,但我需要打开应用程序两次才能询问第二个权限(READ_CONTACT)

我还尝试更改代码:

    if((ContextCompat.checkSelfPermission(MainActivity.this,
        Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(MainActivity.this,
        Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) ) {
    if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
            Manifest.permission.SEND_SMS) && ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
            Manifest.permission.READ_CONTACTS)){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.SEND_SMS},1);
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_CONTACTS},1);

    } else {
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.SEND_SMS},1);
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_CONTACTS},1);
    }
} else {
    // do nothing
} 

我知道它看起来很乱,但它也没有用。

提前感谢愿意提供帮助的人

0 个答案:

没有答案