android - 在Marshmallow 中,如何像Hangouts 那样一一提示两个或多个权限对话框?

标签 android android-6.0-marshmallow android-permissions

我正在处理我的应用程序代码以在 Marshmallow 设备上工作,我正在管理它的权限对话框以在需要的地方显示。

目前遇到了需要两个权限(位置和存储)的场景,我想像环聊一样一一询问。找不到它是如何定制的,有什么解决方案吗?

enter image description here enter image description here

这是我为单一权限处理的代码:

case REQUEST_CODE_WRITE_EXTERNAL_STORAGE: {

    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        /Permission is granted
        Toast.makeText(this, "SDK >= 23 & permission Granted ", Toast.LENGTH_SHORT).show();
        return true;

    } else {
        //Permission is revoked
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE);
        return false;
    }
}

并且在 onRequestPermissionsResult() 中:

case REQUEST_CODE_WRITE_EXTERNAL_STORAGE: {

    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // permission was granted, yay! Do the
        // contacts-related task you need to do.
        Log.e("PMS", "granted");
        Toast.makeText(this, "SDK >= 23 & permission Granted ", Toast.LENGTH_SHORT).show();

    } else {
        Log.e("PMS", "Not Granted");

        // permission denied, boo! Disable the
        // functionality that depends on this permission.
        int checkStatus = getPermissionStatus(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (checkStatus == 3) {
            Toast.makeText(this, "SDK  >= 23 & permission Denied ", Toast.LENGTH_SHORT).show();

        } else if (checkStatus == 4) {
            Toast.makeText(this, "SDK  >= 23 & permission Blocked ", Toast.LENGTH_SHORT).show();

        }
    }
    return;
}

最佳答案

要一一请求多个权限,您需要在第二个参数 String[] ActivityCompat.requestPermissions 方法中添加所有权限。像这样:

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE);

如果有帮助请告诉我

关于android - 在Marshmallow 中,如何像Hangouts 那样一一提示两个或多个权限对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36470550/

相关文章:

Android Jetpack 撰写 : Can't set backgroundColor for OutlinedTextField

onRequestPermissionsResult 中的 java.lang.StackOverflowError (Android)

android - 我得到 : java. lang.SecurityException: "gps"location provider requires ACCESS_FINE_LOCATION permission

android - 在风景中将 Camera Activity 旋转 180 度会导致反转相机预览

android - 使用Qt时如何在android中阻止后退键

Android M 权限关闭我的应用程序

android - 当设备休眠几个小时时警报不会响起(在 Marshmallow 上)

android - 从 list 中删除后,应用权限显示在应用设置中

Android 计时码表格式

fingerprint - 为多个用户使用来自 Android 的新指纹 API