PermissionDispatcher Plugin Demo
PermissionDispatcher Plugin
It is very convenient to add automatic application authority code. If the user chooses to agree, it can be used directly. If the user refuses to accept
1. Add plug-ins to Android Studio
Method 1: download on the official website
https://plugins.jetbrains.com/plugin/8349-permissionsdispatcher-plugin
Mode 2: Download offline
The permission plugin (version 1.4.7) is stored in the "app/plugin /" directory of this project
2. Add a test permission in Android Studio
Take the application of camera as an example:
3. Add comment code
Right click - menu select generate - menu select generate running permission
4. Add in auto generated code
@NeedsPermission({Manifest.permission.CAMERA}) void requestCameraPermission() { Log.i(TAG, "requestCameraPermission succeed... "); Toast.makeText(this,"Opening camera permission succeeded", Toast.LENGTH_LONG).show(); } @OnPermissionDenied(Manifest.permission.CAMERA) void cameraPermissionDenied() { Log.i(TAG, "deniedCameraPermission ... "); } @OnNeverAskAgain(Manifest.permission.CAMERA) void cameraPermissionNerverAskAgain() { Log.i(TAG, "onCameraNeverAskAgain ... "); new AlertDialog.Builder(this) .setPositiveButton("I will open it myself", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivity(intent); } }) .setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .setCancelable(false) .setMessage("Camera permission is required, your majesty. You have set "reject" to turn on, and checked "no more reminders"") .show(); }
5. Add processing results manually
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); Log.i(TAG, "onRequestPermissionsResult requestCod = " + requestCode + ", grantResults = " + grantResults); MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); }
6. Write like this after being called (such as clicking a button)
findViewById(R.id.id_auto_request_permission).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.i(TAG, "call requestCameraPermission 1 "); MainActivityPermissionsDispatcher.requestPermissionWithCheck(MainActivity.this); Log.i(TAG, "call requestCameraPermission 2 "); } });
7, result
8, summary
If there is no permission for the permission added through this method, the function annotated as requestCameraPermission will not be executed to ensure security. demo:
- If the user refuses to accept, the next call will also apply for permission
- If the user selects "don't remind again" and calls the function that needs permission again, it will also prompt. At this time, guide the user to open the permission manually and jump to the setting page directly
Github Code: https://github.com/HungryGoogle/PermissionDispatcherPluginDemo