Android6.0以上动态授权实现

1、定义与权限相关的变量

AndroidManifest.xml
    
    
    
    

定义相关变量与Manifest.xml对应

    //权限申明
    private String[] permissions = {
            Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
   private AlertDialog permissionDialog;

2、权限检查

    public void isgettingPermission(){
        //版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M){
            int a = ContextCompat.checkSelfPermission(getApplicationContext(),permissions[0]);
            int b= ContextCompat.checkSelfPermission(getApplicationContext(),permissions[1]);
            int c= ContextCompat.checkSelfPermission(getApplicationContext(),permissions[2]);
            int d= ContextCompat.checkSelfPermission(getApplicationContext(),permissions[3]);
            if (a != PackageManager.PERMISSION_GRANTED || b != PackageManager.PERMISSION_GRANTED||
                    c!= PackageManager.PERMISSION_GRANTED || d!= PackageManager.PERMISSION_GRANTED ){
                //请求权限
                ActivityCompat.requestPermissions(MainActivity.this,permissions,321);
            }
        }
    }

3、请求权限回调

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 321){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                //判断是不是用户点击
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    boolean b = shouldShowRequestPermissionRationale(permissions[0]);
                    if (b){
                        showDialogTipUserGoToAppSetting();
                    }else {
                        finish();
                    }
                }else {
                    //获取权限成功
                    Log.i("mouse","get Permission Successful!!!");
                }
            }
        }
    }

4、手动设置权限

    private void showDialogTipUserGoToAppSetting() {
        permissionDialog = new AlertDialog.Builder(this).setTitle("权限不可用")
        .setMessage("请在-应用设置-权限-中,允许应用使用权限")
        .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                goToAppSetting();
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        }).setCancelable(false).show();
    }
   

5、跳转APP设置

    private void goToAppSetting(){
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package",getPackageName(),null);
        startActivityForResult(intent,123);
    } 

6、应用设置里面的对权限设置的回调方法

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == 123){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                int a = ContextCompat.checkSelfPermission(getApplicationContext(),permissions[0]);
                int b= ContextCompat.checkSelfPermission(getApplicationContext(),permissions[1]);
                int c= ContextCompat.checkSelfPermission(getApplicationContext(),permissions[2]);
                int d= ContextCompat.checkSelfPermission(getApplicationContext(),permissions[3]);
                if (a != PackageManager.PERMISSION_GRANTED || b != PackageManager.PERMISSION_GRANTED||
                        c!= PackageManager.PERMISSION_GRANTED || d!= PackageManager.PERMISSION_GRANTED ){
                  showDialogTipUserGoToAppSetting();
                }else {
                    if (permissionDialog != null && permissionDialog.isShowing()){
                        permissionDialog.dismiss();
                    }
                }
            }
        }
    }

动态权限获取完毕

你可能感兴趣的:(Android,Studio)