android 动态权限处理

private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
 private AlertDialog dialog;

第一步: 判断手机Version

if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
    //检查是否已经开启了权限
    int checkSelfPermission = ContextCompat.checkSelfPermission(this, permissions[0]);
    if (checkSelfPermission!= PackageManager.PERMISSION_GRANTED){
        //没有权限去申请权限
        showDialogPermission();
    }
}
第二步:提示用户开启权限

private void showDialogPermission() {
     new AlertDialog.Builder(this)
                    .setTitle("存储权限不可用")
                     .setMessage("由于app需要获取存储空间,为你存储个人信息;\n否则,您将无法正常使用app")
                     .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int which) {
                                     startRequestPermission();
                                 }
                     })
                   .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int which) {
                                     dialog.dismiss();
                                 }
                       })
            .setCancelable(false)
            .show();
}

private void startRequestPermission() {
    ActivityCompat.requestPermissions(this,permissions,1);
}
第三步: 权限申请回调结果处理

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode==1){
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
            if (grantResults[0]!=PackageManager.PERMISSION_GRANTED){
                // 判断用户是否 点击了不再提醒
                boolean b = shouldShowRequestPermissionRationale(permissions[0]);
                if (!b){//点击了不再提醒
                // 提示用户去应用设置界面手动开启权限
                    showDialogTipUserGoToAppSettting();
                }
            }else {
                Toast.makeText(this, "申请权限成功", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
第四步:点击不再提醒的处理

private void showDialogTipUserGoToAppSettting() {
    dialog = new AlertDialog.Builder(this)
                    .setTitle("存储权限不可用")
                    .setMessage("请在-应用设置-权限-中,允许app使用存储权限来保存用户数据")
                   .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                     // 跳转到应用设置界面
                                    goToAppSetting();
                                 }
                   })
                     .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                           public void onClick(DialogInterface dialog, int which) {
                                   dialog.dismiss();
                                 }
                       }).setCancelable(false).show();
}

private void goToAppSetting() {
            Intent intent = new Intent();
             intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
             Uri uri = Uri.fromParts("package", getPackageName(), null);
             intent.setData(uri);
            startActivityForResult(intent, 2);
}
//
     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == 2) {
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                     // 检查该权限是否已经获取
                            int i = ContextCompat.checkSelfPermission(this, permissions[0]);
                                 // 权限是否已经 授权
                                 if (i != PackageManager.PERMISSION_GRANTED) {
                                 // 提示用户开启权限
                                     showDialogTipUserGoToAppSettting();
                                 } else {
                                     if (dialog != null && dialog.isShowing()) {
                                           dialog.dismiss();
                                         }
                                     Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
                                 }
                     }
               }
        }


你可能感兴趣的:(android 动态权限处理)