Android Study 之聊聊权限那点事儿~别怕,就是干~!!!( 七 )


编译时,r为我们([活动名称] + r)生成一个类,我们可以使用该类来安全地访问这些受权限保护的方法,而我们唯一要做的就是将权限处理委托委托给这个帮助类,那么接下来我们一起来看看官方提供的小例子 。
// This file was generated by PermissionsDispatcher. Do not modify!package permissions.dispatcher.sample;import android.support.v4.app.ActivityCompat;import permissions.dispatcher.PermissionRequest;import permissions.dispatcher.PermissionUtils;import java.lang.ref.WeakReference;final class MainActivityPermissionsDispatcher {private static final int REQUEST_SHOWCAMERA = 0;private static final String[] PERMISSION_SHOWCAMERA = new String[] {"android.permission.CAMERA"};private MainActivityPermissionsDispatcher() {}static void showCameraWithCheck(MainActivity target) {if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA)) {target.showCamera();} else {if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SHOWCAMERA)) {target.showRationaleForCamera(new ShowCameraPermissionRequest(target));} else {ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA);}}}static void onRequestPermissionsResult(MainActivity target, int requestCode, int[] grantResults) {switch (requestCode) {case REQUEST_SHOWCAMERA:if (PermissionUtils.verifyPermissions(grantResults)) {target.showCamera();} else {if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SHOWCAMERA)) {target.onCameraNeverAskAgain();} else {target.onCameraDenied();}}break;default:break;}}private static final class ShowCameraPermissionRequest implements PermissionRequest {private final WeakReference weakTarget;private ShowCameraPermissionRequest(MainActivity target) {this.weakTarget = new WeakReference(target);}@Overridepublic void proceed() {MainActivity target = weakTarget.get();if (target == null) return;ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA);}@Overridepublic void cancel() {MainActivity target = weakTarget.get();if (target == null) return;target.onCameraDenied();}}}
初步了解之后,还是来点实际的吧 。撸码,走起~
1.老规矩 查看运行结果:测试设备为乐视
1-1 用户拒绝

Android Study 之聊聊权限那点事儿~别怕,就是干~!!!

文章插图
1-2 用户允许
Android Study 之聊聊权限那点事儿~别怕,就是干~!!!

文章插图
2.添加依赖
compile 'com.github.hotchemi:permissionsdispatcher:2.3.2'annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.3.2'
3.编写基础类
package cn.hlq.androidrightsmanagement.permissionsdispatcher;import android.Manifest;import android.app.Activity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.v7.app.AlertDialog;import android.view.View;import android.widget.Toast;import cn.hlq.androidrightsmanagement.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;/*** permissionsdispatcher简单使用 create by heliquan at 2017年6月18日*/@RuntimePermissions // 标记需要运行时判断的类(用于动态生成代理类)public class DispatcherActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_permissions_dispatcher);findViewById(R.id.id_c).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {}});}@NeedsPermission(Manifest.permission.CAMERA)// 标记需要检查权限的方法void showToast() {startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), 200);}@OnShowRationale(Manifest.permission.CAMERA)// 授权提示回调void showRationale(final PermissionRequest request) {new AlertDialog.Builder(this).setMessage("不给权限你试试~").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {request.proceed();//请求权限}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {request.cancel();}}).setCancelable(false).show();}@OnPermissionDenied(Manifest.permission.CAMERA)// 授权被拒绝回调void denied() {Toast.makeText(this, "丫的,不给我权限!", Toast.LENGTH_SHORT).show();}@OnNeverAskAgain(Manifest.permission.CAMERA)// 授权被拒绝并不再提醒回调void neverAskAgain() {Toast.makeText(this, "丫的,还点击不再询问,fuck!", Toast.LENGTH_SHORT).show();}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);}}