美文网首页
android权限管理之PermissionsDispatche

android权限管理之PermissionsDispatche

作者: Oort | 来源:发表于2018-07-06 10:50 被阅读0次

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.重构项目

相关文章

  • android权限管理之PermissionsDispatche

    android权限管理之PermissionsDispatcher使用 使用开源管理框架 使用方法 1.使用And...

  • Android6.0动态权限笔记

    参考: Android Training之权限管理 Android 6.0 RuntimePermission--...

  • [Android]USB开发

    声明:主要参照文档:Android开发之USB数据通信安卓USB通信之权限管理 第一:请求权限和请求权限回调(通过...

  • PermissionsDispatcher动态权限支持库使用笔记

    Android6.0谷歌加强了对android权限机制等管理,对敏感权限要动态加载。为了便于管理动态权限,GitH...

  • Android 6.0动态权限

    权限管理的背景 Android6.0(API23)之前,用户在安装应用的时候会产生一个权限列表,只有允许这些权限之...

  • 简单的权限申请工具类

    最近由于工作,需要对老项目进行项目改造,在改造过程中,项目使用的权限申请为PermissionsDispatche...

  • Android 摘要

    Android 摘要 Android应用权限管理 Understanding App PermissionsAnd...

  • Android 6.0 的权限管理

    关于权限管理 Android6.0 发布之后,Android 的权限系统被重新设计。在 23 之前 App 的权限...

  • Android M 运行时权限

    Android 6.0 版本(Api 23)推出了 动态权限管理。 应用权限简介 Android应用默认情况下不拥...

  • Android运行权限管理

    为什么会有运行权限管理 运行权限管理和Android M(6.0 API23)之前的权限管理有什么区别 国内手机厂...

网友评论

      本文标题:android权限管理之PermissionsDispatche

      本文链接:https://www.haomeiwen.com/subject/bhevuftx.html