Android API 23请求多个权限 [英] Android API 23 Requesting Multiple Permissions

查看:97
本文介绍了Android API 23请求多个权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试请求启动器活动的权限.对于API< 23,它运作完美.但是,当我在运行API 23的设备上测试该应用程序时,它显示:"PostPaid Balance已停止."我点击关闭应用程序按钮",该应用程序关闭并立即请求一个许可.我点击接受.然后,我点击应用程序图标以重新打开,发生了同样的事情,除了现在它要求下一个许可.然后,我点击应用程序图标,这一次可以正确执行. 好像是一次请求一个权限.关于如何解决这个问题有什么想法吗?

I am trying to request permissions on my Launcher Activity. For API < 23, it works perfect. However, when I test the app on a device running API 23, it says: "PostPaid Balance has stopped." I hit the "close App button," the app closes and immediately asks for one permission. I hit accept. Then I tap on the app icon to reopen and the same thing happens, except that now it asks for the next permission. Then I tap on the app icon and this time executes correctly. It seems like it is asking for permissions one at a time. Any ideas on how to go about this?

// Below code is implemented on onCreate() of the launcher activity.
 if (Build.VERSION.SDK_INT < 23) {
        ActivityCompat.checkSelfPermission(this.getApplicationContext(), "android.permission.READ_SMS");
        ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_CALL_LOG);
        ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_PHONE_STATE);

        if ((ActivityCompat.checkSelfPermission(this, "android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED)) {
            requestSmsPermission();
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            requestPhoneStatePermission();
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            requestCallLogPermission();
        }
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if ((this.checkSelfPermission("android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED) &&
                (this.checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) &&
                (this.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) {
            this.requestPermissions(new String[]{"android.permission.READ_SMS", Manifest.permission_group.PHONE}, REQUEST_SMS);

        }
    }

推荐答案

要一次性回答多个权限问题,请将权限添加到字符串数组中.在此示例中,我想请求READ_PHONE_STATE和WRITE_EXTERNAL_STORAGE的权限:

To answer your question on how to request for multiple permissions in one go, add permissions to your array of strings. In this example, I want to request permissions for READ_PHONE_STATE and WRITE_EXTERNAL_STORAGE:

ArrayList<String> arrPerm = new ArrayList<>();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    arrPerm.add(Manifest.permission.READ_PHONE_STATE);
}
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!arrPerm.isEmpty()) {
    String[] permissions = new String[arrPerm.size()];
    permissions = arrPerm.toArray(permissions);
    ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}

现在,检查授予了哪些权限:

Now, check which permissions were granted:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0) {
                for(int i = 0; i < grantResults.length; i++) {
                    String permission = permissions[i];
                    if(Manifest.permission.READ_PHONE_STATE.equals(permission)) {
                        if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            // you now have permission
                        }
                    }
                    if(Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
                        if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            // you now have permission
                        }
                    }
                }
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            break;
        }
    }

    // other 'case' lines to check for other
    // permissions this app might request
}

这篇关于Android API 23请求多个权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆