如何在我的代码中在运行时android M中一次授予多个权限

时间:2016-05-27 05:40:24

标签: android permissions android-6.0-marshmallow android-6.0.1-marshmallow

我想一次授予多个权限.....这是我的代码..这只是我想要一次授予多个权限的示例..

public class MainActivity extends Activity {

public static final int R_PERM = 123;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    if ((CheckPermission(this, Manifest.permission.CAMERA))&&
         (CheckPermission(this, Manifest.permission.READ_PHONE_STATE))&&
         (CheckPermission(this, Manifest.permission.NFC)))
    {
        PermHandling();
    }

    else {
        RequestPermission(MainActivity.this, Manifest.permission.CAMERA, R_PERM);
        RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, R_PERM );
        RequestPermission(MainActivity.this, Manifest.permission.NFC, R_PERM );

        //NewPermHandling();
    }

}
private void PermHandling() {
    //My app internal parts....
    //Here my stuff works...
}

//private void NewPermHandling(){

//}

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

    switch (permsRequestCode) {

        case R_PERM: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                PermHandling();
            } else {
                //Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}

public void RequestPermission(Activity thisActivity, String Permission, int Code) {
    if (ContextCompat.checkSelfPermission(thisActivity,
            Permission)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Permission)) {
        } else {
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Permission},
                    Code);
        }
    }
}

public boolean CheckPermission(Context context, String Permission) {
    if (ContextCompat.checkSelfPermission(context,
            Permission) == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

}

这里我逐个授予权限所以我在运行时逐个获取权限请建议我立即授予所有权限..

请在我的代码上建议我应该在哪里更改..

这里我的我的应用程序我有更多权限,我需要立即给予相机,位置,存储等...请在Mycode上建议我....

更新 并且所有权限都应该与 从不再询问 检查按钮一起....

请在我的代码中告诉我这个,,,,

2 个答案:

答案 0 :(得分:-1)

在您的Request Permision方法中,而不是一次发送一个权限,您可以要求多次使用。

     ActivityCompat.requestPermissions(thisActivity,
                new String[]{Permission1,Permission2,Permission3},
                Code);

Permission1 = Manifest.permission.CAMERA Permission2 = Manifest.permission.READ_PHONE_STATE Permission3 = Manifest.permission.NFC

这样做是因为它让android在对话框中一个接一个地请求权限。您可以通过运行int [] grantResults

来检查onRequestPermissionsResult中一次授予的权限。

请注意,int [] grantResults以请求的权限顺序返回权限授予结果。在我们的例子中 - Manifest.permission.CAMERA ,Manifest.permission.READ_PHONE_STATE, Manifest.permission.NFC

请查看下面详细解释的链接。

https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:-1)

您可以创建权限数组并将其直接传递给requestPermissions()

String[] PERMISSIONS = new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.RECORD_AUDIO};

并将其传递给requestPermissions()

requestPermissions(PERMISSIONS, 10);

并检查您是否获得onRequestPermissionsResult

中的所有权限
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    switch (requestCode) {
        case 10:
            if (hasAllPermissionsGranted(grantResults)) {
                // Permission Granted

            } else {
                // Permission Denied

            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

以下是hasAllPermissionsGranted方法

public boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
    for (int grantResult : grantResults) {
        if (grantResult == PackageManager.PERMISSION_DENIED) {
            return false;
        }
    }
    return true;
}

要获得另一种处理多项权限的方法,请检查Requesting Runtime Permission – Part 2