美文网首页
RxPermissions库搞定Android6.0权限

RxPermissions库搞定Android6.0权限

作者: 水大云霄 | 来源:发表于2016-12-16 11:26 被阅读649次

    1、工具的github地址https://github.com/tbruyelle/RxPermissions

    2、android studio中引用:

    repositories {
        jcenter() // If not already there
    }
    dependencies { 
        compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.1@aar'
    }
    

    3在代码中的使用

    一、用之前首先在BaseActivity中的onCreate中实例化 代码:RxPermissions rxPermissions = new RxPermissions(this);
    
    二、在用到权限的地方进行申请
    ######申请单个权限
    rxPermissions .request(Manifest.permission.CAMERA) 
    .subscribe(granted -> {
     if (granted) { 
           // Always true pre-M // I can control the camera now
     } else { 
        // Oups permission denied
     }
     });
    
    ######申请多个权限
    rxPermissions .request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE) 
    .subscribe(granted -> {
     if (granted) { 
           //全部通过
          // All requested permissions are granted
     } else { 
         //有一个没通过
        // At least one permission is denied
     }
     });
    
    
    ######申请多个权限,分个
    rxPermissions
        .requestEach(Manifest.permission.CAMERA,
                 Manifest.permission.READ_PHONE_STATE)
        .subscribe(permission -> { // will emit 2 Permission objects
            if (permission.granted) {
               // `permission.name` is granted !
            } else if (permission.shouldShowRequestPermissionRationale)
               // Denied permission without ask never again
            } else {
               // Denied permission with ask never again
               // Need to go to the settings
            }
        });
    

    注:现在很多手机厂商对android6.0的rom进行了定制修改,权限申请有的可能已经免了。

    相关文章

      网友评论

          本文标题:RxPermissions库搞定Android6.0权限

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