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();
                                 }
                     }
               }
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章