android权限管理之PermissionsDispatcher使用

使用开源管理框架

使用方法

  • 1.使用Android studio 2.2以上版本

  • 2.添加依赖 到项目的应用gradle文件

       compile 'com.github.hotchemi:permissionsdispatcher:2.3.2'   annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.3.2'
  • 3.示例代码

       @RuntimePermissions   public class MainActivity extends AppCompatActivity {   private AlertDialog alert = null;   private AlertDialog.Builder builder = null;   private Button btnRe;   @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      btnRe = (Button) this.findViewById(R.id.request_permission);      btnRe.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivity.this);          }      });   }   @NeedsPermission(Manifest.permission.CAMERA)   void showCamera() {      alert = null;      builder = new AlertDialog.Builder(this);      alert = builder.setIcon(R.mipmap.ic_launcher_round)              .setTitle("成功:")              .setMessage("拿到了相机权限")              .setNegativeButton("取消", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      Toast.makeText(MainActivity.this, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();                  }              })              .setPositiveButton("确定", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      Toast.makeText(MainActivity.this, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();                  }              })              .setNeutralButton("中立", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      Toast.makeText(MainActivity.this, "你点击了中立按钮~", Toast.LENGTH_SHORT).show();                  }              }).create();             //创建AlertDialog对象        alert.show();                    //显示对话框     }     @OnShowRationale(Manifest.permission.CAMERA)     void showRationaleForCamera(final PermissionRequest request) {      alert = null;      builder = new AlertDialog.Builder(this);      alert = builder.setIcon(R.mipmap.ic_launcher_round)              .setTitle("提示:")              .setMessage("您的应用必须要获得相机权限,否则无法正常工作")              .setNegativeButton("取消", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      request.cancel();                  }              })              .setPositiveButton("同意", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      request.proceed();                  }              }).create();             //创建AlertDialog对象           alert.show();                    //显示对话框        }        @OnPermissionDenied(Manifest.permission.CAMERA)       void showDeniedForCamera() {       alert = null;       builder = new AlertDialog.Builder(this);       alert = builder.setIcon(R.mipmap.ic_launcher_round)              .setTitle("提示:")              .setMessage("您拒绝了权限,是否重新请求权限")              .setNegativeButton("取消", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                  }              })              .setPositiveButton("确定", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivity.this);                  }              }).create();             //创建AlertDialog对象        alert.show();                    //显示对话框        }  @OnNeverAskAgain(Manifest.permission.CAMERA)  void showNeverAskForCamera() {      alert = null;      builder = new AlertDialog.Builder(this);      alert = builder.setIcon(R.mipmap.ic_launcher_round)              .setTitle("提示:")              .setMessage("您彻底拒绝了权限,现在是否打开设置去激活?")              .setNegativeButton("取消", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                  }              })              .setPositiveButton("去激活", new DialogInterface.OnClickListener() {                  @Override                  public void onClick(DialogInterface dialog, int which) {                      Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);                      intent.setData(Uri.fromParts("package", getPackageName(), null));                      startActivity(intent);                  }              }).create();             //创建AlertDialog对象      alert.show();                    //显示对话框  }  @Override  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {      super.onRequestPermissionsResult(requestCode, permissions, grantResults);      MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);  }  }  
  • 4.重构项目

更多相关文章

  1. Android消息提示框和对话框
  2. Android(安卓)Model正确使用姿势——AutoValue
  3. Android(安卓)之 Bitmap 和 File 相互转换
  4. android的唯一性
  5. 【Android(安卓)framework】AndroidManagerService初始化流程
  6. Android番外03_Umeng友盟统计集成
  7. DelphiXE Android的所有权限按照分类总结说明
  8. Android(安卓)获取网络状态的工具类
  9. Android中如何使用自定义对话框

随机推荐

  1. android:gravity 和 android:layout_Grav
  2. android 电容屏(三):驱动调试之驱动程序分析
  3. 跟我学Android(安卓)NDK开发(一)
  4. Android菜单详解(二)——创建并响应选项菜
  5. Android和PHP开发最佳实践
  6. android的测试工具CTS
  7. UI布局参数(持续更新)
  8. Android(安卓)SDK中 tools 工具介绍
  9. Android窗口治理机制
  10. Android应用资源总结二:Android的问号?和@