PermissionDispatcher 运行时权限框架

第一步在app的build.gradle文件中添加:

dependencies {
  // PermissionDispatcher 框架的使用
    implementation 'com.github.hotchemi:permissionsdispatcher:2.1.3'
    annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.1.3'
    implementation 'com.android.support:support-v4:28.0.0'   
}

在activity中

package com.xiaozhuyisheng.jinjiedemos.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Toast;

import com.xiaozhuyisheng.jinjiedemos.BaseActivity;
import com.xiaozhuyisheng.jinjiedemos.R;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

// 必须的注解,它用来注册一个Activity或Fragment使他们可以处理权限
@RuntimePermissions
public class PermissionDemoActivity extends BaseActivity {

    /**
     * @RuntimePermissions:必须的注解,它用来注册一个Activity或Fragment使它们可以处理权限
     * @NeedsPermission:必须的注解,在需要获取权限的地方注解,用来获取权限
     * @OnShowRationale:提示用户为何要开启权限,在用户选择拒绝后,再次需要访问该权限时调用
     *@OnPermissionDenied:用户选择拒绝时的提示
     * @OnNeverAskAgain:用户选择不再询问后的提示
     */


    @Override
    protected int initLayoutID() {
        return R.layout.activity_permission;
    }

    @Override
    protected void initLogic() {
        // 拨打电话
        findViewById(R.id.btnCall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PermissionDemoActivityPermissionsDispatcher.callWithCheck(PermissionDemoActivity.this);
            }
        });
    }

    @NeedsPermission(Manifest.permission.CALL_PHONE)
        // 在需要获取权限的地方注释
    void call() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "1337196838");
        intent.setData(data);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        startActivity(intent);
    }

    // 提示用户为何要开启权限
    @OnShowRationale(Manifest.permission.CALL_PHONE)
   void showWhy(final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setMessage("拨打电话时需要该权限")
                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 再次执行权限请求
                        request.proceed();
                    }
                })
                .show();
   }

   // 用户选择拒绝时的提示
   @OnPermissionDenied(Manifest.permission.CALL_PHONE)
   void showDenied() {
       Toast.makeText(this,"用户选择拒绝时的提示",Toast.LENGTH_LONG).show();
   }

   // 用户选择不再询问后的提示
   @OnNeverAskAgain(Manifest.permission.CALL_PHONE)
   void showNotAsk() {
        new AlertDialog.Builder(this)
                .setMessage("该功能需要拨打电话")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .show();
   }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionDemoActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
    }
}

其中:

PermissionDemoActivityPermissionsDispatcher 类重新编译后自动生成
posted @ 2019-02-18 16:10  疯子FK  阅读(998)  评论(0编辑  收藏  举报