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

前端之家收集整理的这篇文章主要介绍了android – 在Marshmallow中,如何像Hangouts一样逐个提示两个或多个权限对话框呢?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在处理我的应用程序代码以在Marshmallow设备中工作,我正在管理其权限对话框以显示在需要的位置.

目前在这种情况下,它需要两个权限(位置和存储),我想逐个询问Hangout的工作方式.无法找到它的定制方式,任何解决方案?

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,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相关文章