Android Marshmallow运行时权限 - 无效

时间:2016-03-30 12:15:10

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

我正在研究Android-M运行时权限。我想为我的一个片段添加一个位置权限。这是我的代码。 Android请求权限,但即使我拒绝该权限,也没有任何反应,它会解除对话框,但activateGPS()仍然有效并返回值。提前谢谢。

if (Build.VERSION.SDK_INT >= 23) {
        // Marshmallow+
        getPermissionToReadUserContacts();
    } else {
        // Pre-Marshmallow
        System.out.println("CODMOB: Device not marshmallow");
        activateGPS();
    }


    activateGPS();

    return view;
}
private static final int LOCATION_PERMISSIONS_REQUEST = 1;
public void getPermissionToReadUserContacts() {
    System.out.println("CODMOB: getPermissionToReadUserContacts()");
    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
    // checking the build version since Context.checkSelfPermission(...) is only available
    // in Marshmallow
    // 2) Always check for permission (even if permission has already been granted)
    // since the user can revoke permissions at any time through Settings

        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // The permission is NOT already granted.
            // Check if the user has been asked about this permission already and denied
            // it. If so, we want to give more explanation about why the permission is needed.
            System.out.println("CODMOB: The permission is NOT already granted.");
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.ACCESS_COARSE_LOCATION)) {
                // Show our own UI to explain to the user why we need to read the contacts
                // before actually requesting the permission and showing the default UI
                System.out.println("CODMOB: We need to access location.");
            }else{

            // Fire off an async request to actually get the permission
            // This will show the standard permission request dialog UI
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    LOCATION_PERMISSIONS_REQUEST);

            }
        }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == LOCATION_PERMISSIONS_REQUEST) {
        if (grantResults.length>0 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            System.out.println("CODMOB:permission granted");
            activateGPS();
            //Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
        } else {
            System.out.println("CODMOB: permission denied");
            //Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

3 个答案:

答案 0 :(得分:1)

将您的代码更改为

if (Build.VERSION.SDK_INT >= 23) {
        // Marshmallow+
        getPermissionToReadUserContacts();
    } else {
        // Pre-Marshmallow
        System.out.println("CODMOB: Device not marshmallow");
        activateGPS();
    }
    return view;
}

尽管有片段....也在你的活动中添加这个方法

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.containerView);
        if (fragment != null) {
            fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

答案 1 :(得分:0)

 ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},       100);

 @Override
 public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults)     
switch (requestCode) {
    case 100: {

      // 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.          
        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
        return;
      }

    // other 'case' lines to check for other
    // permissions this app might request
    }
}

答案 2 :(得分:0)

 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) !=
            PackageManager.PERMISSION_GRANTED) {
        final String[] permission = new String[]{
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.INTERNET,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.PACKAGE_USAGE_STATS,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
        };
        ActivityCompat.requestPermissions(this, permission, 0);
    }

在您的活动中使用上述代码可能有所帮助。