Method of android 6.0 Permission Dynamic Application Framework permissiondispatcher
- 2021-08-17 01:00:48
- OfStack
1. Add dependencies
Add in the build. gradle file of project
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
Add in build. gradle of module
apply plugin: 'android-apt'
. . . . . . .
dependencies {
. . . .
compile 'com.github.hotchemi:permissionsdispatcher:2.3.1'
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.3.1'
}
2. Add comments
package com.shilian.yibo;
import ....
/**
* Main page
*/
@RuntimePermissions
public class MainActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
.....
}
@OnClick(R.id.textView_scan_code_home)
public void onClick(View view) {
switch (view.getId()){
case R.id.textView_scan_code_home:
MainActivityPermissionsDispatcher.startScanWithCheck(this);
break;
}
}
@NeedsPermission(android.Manifest.permission.CAMERA)
void startScan() {
startActivityForResult(new Intent(this, CaptureActivity.class), 1);
}
/**
* OnShowRationale Annotate the method of giving prompt when getting permission
* */
@OnShowRationale(android.Manifest.permission.CAMERA)
void showRationaleForCamera(final PermissionRequest request) {
new AlertDialog.Builder(this).setPositiveButton(" Got it ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();
}
}).setMessage(" Scan code requires camera permission ").setCancelable(true).show();
}
/**
* OnPermissionDenied Method of callback when annotation denies authorization
* */
@OnPermissionDenied(android.Manifest.permission.CAMERA)
void onCameraDenied() {
toast(" Camera permissions are not turned on ");
}
/**
* OnNeverAskAgain Annotate the callback method when the user chooses to refuse and no longer ask
* */
@OnNeverAskAgain(android.Manifest.permission.CAMERA)
void onCameraNeverAskAgain() {
toast(" Camera permissions are not turned on ");
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
}
}
Note description
RuntimePermissions: Required comment to comment on activity or fragment for which permission is to be obtained
NeedsPermission: Required annotation to annotate the method that needs to get permission
OnShowRationale: An unnecessary comment, prompting the user why this permission should be turned on. It is called when the user needs access permission after refusing it
OnPermissionDenied: Non-required comment, prompt when user chooses to reject
OnNeverAskAgain: Non-required comments, prompts after the user chooses not to ask again
Call to get permission: The auxiliary class xxxPermissionsDispatcher will be generated from the new compiler, which will be called through xxxPermissionsDispatcher. xxxWithCheck method, and the onRequestPermissionsResult method will be overridden, in which onRequestPermissionsResult method of onRequestPermissionsResult will be called, and the result of getting permission will be returned.