1. 程式人生 > >Android 7.0 獲取自定義許可權

Android 7.0 獲取自定義許可權

private String[] permissions = {Manifest.permission.CAMERA,
      Manifest.permission.WRITE_EXTERNAL_STORAGE,
   // Manifest.permission.ACCESS_COARSE_LOCATION,
      Manifest.permission.GET_ACCOUNTS,
      Manifest.permission.ACCESS_FINE_LOCATION,
      Manifest.permission.RECORD_AUDIO,
      Manifest.permission.CALL_PHONE,
   // Manifest.permission.READ_EXTERNAL_STORAGE
};
String content[]={"拍照和錄製視訊許可權","讀寫儲存卡許可權","獲取手機賬戶列表許可權","手機定位許可權","錄製音訊許可權","打電話許可權"};
private void checkPermissions() {
   if (Build.VERSION.SDK_INT >= 23) {
      ActivityCompat.requestPermissions(this, permissions, 321);

   }
}
StringBuilder sb_error=null;
StringBuilder sb_wrong=null;

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
   super.onRequestPermissionsResult(requestCode, permissions, grantResults);
   if (requestCode == 321) {
      if (Build.VERSION.SDK_INT >= 23) {
         int index = 0;
         sb_error = new StringBuilder();
         for (int i = 0; i < permissions.length; i++) {
            /*if (permissions[i] == "Manifest.permission.READ_EXTERNAL_STORAGE" || permissions[i] == "Manifest.permission.ACCESS_COARSE_LOCATION")
              {continue;}*/
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
               if (shouldShowRequestPermissionRationale(permissions[i])) {
                  sb_error.append(content[i]);
                  sb_error.append(" ");
                  index++;
               }
            }
         }
         if (index != 0) {
            setAuthority(sb_error.toString());
            index = 0;
         }
      }
   }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (requestCode == 123) {
      if (Build.VERSION.SDK_INT >= 23) {
         sb_wrong = new StringBuilder();
         int index = 0;
         for (int i = 0; i < permissions.length; i++) {
            /*if (permissions[i] == "Manifest.permission.READ_EXTERNAL_STORAGE" || permissions[i] == "Manifest.permission.ACCESS_COARSE_LOCATION")
            {continue;}*/
            int checkper = ContextCompat.checkSelfPermission(GuideActivity.this, permissions[i]);
            {
               if (checkper != PackageManager.PERMISSION_GRANTED){
                  sb_wrong.append(content[i]);
               sb_wrong.append(" ");
               index++;
            }
         }
      }
         if (index != 0) {
            setAuthority(sb_wrong.toString());
            index = 0;
         } else {
            Toast.makeText(this, "獲取所有許可權成功", Toast.LENGTH_SHORT).show();
         }
   }
}
}

public void setAuthority(String content) {
   new AlertDialog.Builder(this)
         .setTitle("還有許可權未設定")
         .setIcon(android.R.drawable.ic_dialog_info)
         .setMessage(content)
         .setPositiveButton("立即設定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               Intent intent = new Intent();
               intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
               Uri uri = Uri.fromParts("package", getPackageName(), null);
               intent.setData(uri);
               startActivityForResult(intent, 123);

            }
         }).setCancelable(false).show();
}