Marshmallow权限应该只询问是否已经授予

时间:2016-09-30 09:18:24

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

我正在开发一个需要多个权限的应用程序,所以我添加了它并且也做了marshmallow代码,但问题是当我在marshmallow重新打开应用程序时它总是要求许可,我只想问它是否没有被授予。

请帮我编写代码,先谢谢!

4 个答案:

答案 0 :(得分:1)

只需检查权限,仅在必要时请求。

的帮助下检查权限
checkSelfPermission(Manifest.permission.READ_CONTACTS)

检查是否已授予权限

if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
    requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant

    return;
}

然后处理权限

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! do the

        } else {
            // permission denied, Disable the
            // functionality that depends on this permission.
        }
        return;
    }
}}

答案 1 :(得分:0)

权限系统已更改。但这里有一个很好的解释如何妥善处理它。

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

这样,只有用户授予权限,对话框才会打开一次。如果他拒绝,您可以选择在下次请求许可时显示解释。

他们还有一个值得阅读的最佳实践部分:

https://developer.android.com/training/permissions/best-practices.html

答案 2 :(得分:0)

public Boolean marshmallowPermission(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkPermission()) {
            Toast.makeText(this,"Permission already granted.", Toast.LENGTH_SHORT).show();
        } else {
            requestPermission();
            //marshmallowRequest();
        }
    }
    return true;
}
@TargetApi(Build.VERSION_CODES.M)
public boolean checkPermission(){

    int phone= checkSelfPermission(android.Manifest.permission.CALL_PHONE);
    if (phone == PackageManager.PERMISSION_GRANTED ){
        return true;
    } else {
        return false;
    }
}

@TargetApi(Build.VERSION_CODES.M)
private void requestPermission(){

    if (shouldShowRequestPermissionRationale(android.Manifest.permission.CALL_PHONE)){

        Toast.makeText(this," Permission must be needed which allows you to access an app. Please allow  in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", this.getPackageName(), null);
        intent.setData(uri);
        this.startActivity(intent);

    } else {

        requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},101);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 101:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(this,"Permission Granted, Now you can access an app.",Toast.LENGTH_LONG).show();

            } else {

                Toast.makeText(this,"Permission Denied, You cannot access call.", Toast.LENGTH_LONG).show();

            }
            break;
    }
}

答案 3 :(得分:0)

请按照以下步骤操作:

  • 创建一组您需要访问的权限。
  • 传递组/数组以检查是否授予权限

供您参考的示例代码:

public static boolean hasPermissions(Context context, String[]permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
    for (String permission : permissions) {
        if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
}
return true;

上面的代码会检查你的权限组,然后将这些权限全部传递给要求

int PERMISSION_ALL = 1; 
//create all permissions array
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, //all your permissions};

//Now check whether permissions are granted or not
if(!hasPermissions(this, PERMISSIONS)){
   ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}

希望这对你有用。