0

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

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 上建议我...。

更新 和所有权限应立即使用“不再询问”检查按钮...。

请在我的代码中建议我这种类型的,,,,

4

2 回答 2

-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

于 2016-05-27T05:52:55.253 回答
-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

于 2016-05-27T05:51:33.983 回答