问多个权限Android

时间:2017-08-02 17:40:49

标签: android permissions

我正在修改现有的Face Tracker应用Android的面部识别示例项目。我遇到了请求多个永久权限的问题。下面的方法是现有方法的修改版本,该方法成功创建了一个弹出窗口以请求摄像机权限。我试图用存储权限复制它,但到目前为止我一直没有成功,我不确定需要在这里更改什么。

 private void requestAllPermissions() {
    Log.w(TAG, "Camera + Storage permissions are not granted. Requesting permissions");

    final String[] permissions = new String[]{Manifest.permission.CAMERA};
    final String[] permissions2 = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};


    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
        return;
    }

    //new
    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        ActivityCompat.requestPermissions(this, permissions2, RC_HANDLE_STORAGE_PERM);
        return;
    }


    final Activity thisActivity = this;

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions,
                    RC_HANDLE_CAMERA_PERM);
        }
    };

    View.OnClickListener listener2 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions2,
                    RC_HANDLE_STORAGE_PERM);
        }
    };

    Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale,
            Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.ok, listener)
            .show();

    Snackbar.make(mGraphicOverlay, R.string.permission_storage_rationale,
            Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.ok, listener2)
            .show();
}

5 个答案:

答案 0 :(得分:8)

如果要在一个对话框中询问所有权限,则应该只有一个String数组,如下所示:

int ALL_PERMISSIONS = 101;

final String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

ActivityCompat.requestPermissions(this, permissions, ALL_PERMISSIONS);

答案 1 :(得分:1)

为什么要多次请求权限? requestpermission方法接受Permissions数组。要请求任何权限,您可以使用以下代码并添加所需的权限。这是在访问与权限相关的任何数据

之前通过请求它们来处理运行时权限的方法
public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {

    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        return false;
    }
}
else { //permission is automatically granted on sdk<23 upon installation
    Log.v(TAG,"Permission is granted");
    return true;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 0:
            boolean isPerpermissionForAllGranted = false;
            if (grantResults.length > 0 && permissions.length==grantResults.length) {
                for (int i = 0; i < permissions.length; i++){
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
                        isPerpermissionForAllGranted=true;
                    }else{
                        isPerpermissionForAllGranted=false;
                    }
                }

                Log.e("value", "Permission Granted, Now you can use local drive .");
            } else {
                isPerpermissionForAllGranted=true;
                Log.e("value", "Permission Denied, You cannot use local drive .");
            }
            if(isPerpermissionForAllGranted){
                shoro();
            }
            break;
        }
    }

执行此操作后,对于API&gt; = 23的设备,您将在运行时弹出,然后在用户接受权限或拒绝权限后,将调用onRequestPermissionsResult方法。所以在这里你必须处理你的检查是否用户授予了应用程序的权限。如果是,您可以继续使用您的逻辑

答案 2 :(得分:1)

您要做的就是将权限放入一个String数组之类的

int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
        android.Manifest.permission.READ_CONTACTS,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_FINE_LOCATION
};

然后要检查权限,请使用

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

现在最后要做的最后一件事是在oncreate()或onStart()中使用它

if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

答案 3 :(得分:1)

我今天遇到了这个问题,Aman Grover的回答实际上为我工作,您只需遵循以下步骤

final String[] permissions = new String[]{ 
    android.Manifest.permission.READ_CONTACTS, 
    Manifest.permission.READ_EXTERNAL_STORAGE
};

ActivityCompat.requestPermissions(this, permissions, 123);

答案 4 :(得分:0)

这是最简单的解决方案:

将此代码添加到MainActivity的onCreate方法中

//Requesting permissions
        int PERMISSION_ALL = 1;
        String[] PERMISSIONS = {
                android.Manifest.permission.READ_CONTACTS,
                android.Manifest.permission.RECORD_AUDIO,
                android.Manifest.permission.CALL_PHONE,
        };

        if (!hasPermissions(this, PERMISSIONS)) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }

现在,将此功能添加到您的主活动类中

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

您可以在应用程序启动时添加ass所需的许多权限。