Android 6.0 权限管理

在需要特定权限的界面请求权限(可以分开请求各个权限)


 private void requestPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE) ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECEIVE_SMS)) {
            } else {
                ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.RECEIVE_SMS}, 0);
            }
        }
    }

 
  

完成回调

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults){
    switch(requestCode){
        case 0:{
          // 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.
           }
           return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
      }
}

你可能感兴趣的:(中级Android工程师进阶)