美文网首页
Android6.0动态权限.md

Android6.0动态权限.md

作者: 奈落落 | 来源:发表于2017-03-07 17:31 被阅读12次

    一、AndroidManifest.xml 中列出需要的权限

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              package="com.test.vegeta">
    
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
        <application
            ...>
        </application>
    </manifest>
    

    二、判断 Android 系统版本

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //此处做动态权限申请
    } else {
        //低于23 不需要特殊处理
    }
    

    三、申请权限并处理回调

    RxPermission项目地址
    实例化

    RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance
    

    请求权限

    rxPermissions
        .request(Manifest.permission.CAMERA,
                 Manifest.permission.READ_PHONE_STATE)
        .subscribe(granted -> {
            if (granted) {//在已有权限的情况下,granted为true
               // All requested permissions are granted
            } else {
               // At least one permission is denied
            }
        });
    

    相关文章

      网友评论

          本文标题:Android6.0动态权限.md

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