Before also dealt with 6.0 after the authority problem, direct processing is very troublesome. PermissionsDispatcher is a repository for the most PermissionsDispatcher.

The first step is to add various comments

  1. @runtimePermissions annotations on activities and fragments that require permissions

    @RuntimePermissions
    public class StudentChallengeActivity extends BaseActivity implements StudentChallengeContract.View {Copy the code
  2. NeedsPermission annotation After the method that requires permissions, you need to pass in the required permissions

    @NeedsPermission(Manifest.permission.RECORD_AUDIO) void startRecord(ImageView img) { long currentTime = Calendar.getInstance().getTimeInMillis(); If (currentTime-lastClickTime < 500) {ShowToast(" time too short "); } if (! isPress && currWord < adapter.getItemCount() - 1) { img.setImageResource(R.drawable.btn_recod_style2); isPress = true; // adapter.getItem(currWord).setWordColor(2); adapter.notifyDataSetChanged(); / / the recording evalUtil2. BeginRe (sentenceList. Get (currWord). GetEng ()); }}Copy the code
  3. The @onshowrationale annotation is a way to explain to the user why this permission is needed.

    @OnShowRationale(Manifest.permission.RECORD_AUDIO) void showRationaleForRecord(final PermissionRequest request){ new Alertdialog.builder (this).setpositiveButton (" Ok ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { request.proceed(); }}). SetNegativeButton (" do not give ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { request.cancel(); }}).setCancelable(false).setMessage(" Challenge requires recording permission, application will request recording permission ").show(); }Copy the code

    You have to pass a PermissionRequest parameter. request.proceed(); The popover that calls up the system for requesting permissions will execute the request.cancel() method corresponding to @needspermissio; It will execute the @onpermissionDenied method if you want to call the system directly

  4. @onpermissionDenied Method to be called if rejected

    @ OnPermissionDenied (Manifest) permission) RECORD_AUDIO) void showRecordDenied () {ShowToast (" rejected challenges on the tape access will not be able to "); }Copy the code
  5. @onneverAskAgain checked methods that are no longer prompted to be called after prohibition

    @OnNeverAskAgain(Manifest.permission.RECORD_AUDIO) void onRecordNeverAskAgain() { new AlertDialog.Builder(this) SetPositiveButton (" good ", new DialogInterface. An OnClickListener () {@ Override public void onClick (DialogInterface dialog, Int which) {// TODO: 2016/11/10 open system setup permission dialog.cancel(); }}). SetNegativeButton (" Cancel ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); }}).setCancelable(false).setMessage(" You have disabled the recording permission, do you want to enable it now ").show(); }Copy the code

Step 2: Make project




The library automatically generates a class like [Activity Name] + PermissionsDispatcher. When calling a method that uses permissions, instead of calling the method that we added @NeedsperMission directly, call the XXXWithCheck method that this class generates. XXX is the method name with @needsperMission added.

switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
                StudentChallengeActivityPermissionsDispatcher.startRecordWithCheck(StudentChallengeActivity.this,img);
    }Copy the code

Process executed

Permissions are needed to perform [Activity Name] PermissionsDispatcher. XXXWithCheck () and then call @ OnShowRationale annotation method, method will pass parameters in PermissionRequest request

  • Request. Proceed () calls the system request permission popover; If no prompt is checked in the system application popup and rejected, the @onNeverAskAgain method is called
  • Performing request.cancel() calls the @onpermissionDenied method

Address of the PermissionsDispatcher library

Github.com/hotchemi/Pe…