1. 程式人生 > >Android6.0+許可權申請

Android6.0+許可權申請

在啟動頁onCreate()使用方法applyForPermissions();

private String[] permissions;

//許可權申請

 private void applyForPermissions(){
        // android 6.0以上,包括6.0操作相簿屬於危險許可權,需要使用者自己點開
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

         permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.CAMERA};
            mPermissionList = new
ArrayList<>(); mPermissionList.clear(); for (int i = 0; i < permissions.length; i++) { if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) { mPermissionList.add(permissions[i]); } } if
(mPermissionList.isEmpty()) {//未授予的許可權為空,表示都授予了 startMainActivity(); } else {//請求許可權方法 String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//將List轉為陣列 ActivityCompat.requestPermissions(this, permissions, 1); return
; } }else { //小於6.0 startMainActivity(); } }

//Android 6.0以上許可權庫中對應請求的開啟或者拒絕的回撥

 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1){
            int i;
            for (i = 0; i < grantResults.length;i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    //判斷是否勾選禁止後不再詢問
                    boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i]);
                    if (showRequestPermission) {
                        applyForPermissions();
                    } else {
                        showDialogTipUserGoToAppSettting();
                    }
                    break;
                } else {
                   continue;
                }
            }
            if(i == grantResults.length){
                startMainActivity();
            }

        }
    }

//假如沒有開啟許可權則提示使用者去應用設定介面手動開啟許可權

  private void showDialogTipUserGoToAppSettting() {
          dialog = new AlertDialog.Builder(this)
                  .setMessage("請在應用設定-許可權中賦予該有聲相機訪問儲存和麥克風的許可權,否則應用將無法正常工作!")
                  .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) {
                          finish();
                      }
                  }).create();
          dialog.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, 123);
    }

//當前頁面的返回回撥

 @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                 if (requestCode == 123) {

                         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                 // 檢查該許可權是否已經獲取
                             int i;
                             for (i = 0; i < permissions.length; i++) {
                                 if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                                     showDialogTipUserGoToAppSettting();
                                     break;
                                 }else {
                                     continue;
                                 }
                             }
                                  if(i == permissions.length)
                                   {
                                         if (dialog != null && dialog.isShowing()) {
                                                 dialog.dismiss();
                                             }
                                       startMainActivity();
                                     }
                             }
                     }
             }