Android 6.0 - 动态权限管理的解决方案

Android 6.0 - 动态权限管理的解决方案

Android 6.0版本(Api 23)推出了很多新的特性, 大幅提升了用户体验, 同时也为程序员带来新的负担.  动态权限管理就是这样, 一方面让用户更加容易的控制自己的隐私, 一方面需要重新适配应用权限. 时代总是不断发展, 程序总是以人为本, 让我们为应用添加动态权限管理吧! 这里提供了一个非常不错的解决方案, 提供源码, 项目可以直接使用.

Android系统包含默认的授权提示框, 但是我们仍需要设置自己的页面. 原因是系统提供的授权框, 会有不再提示的选项. 如果用户选择, 则无法触发授权提示. 使用自定义的提示页面, 可以给予用户手动修改授权的指导.

本文示例的GitHub下载地址

在Api 23中, 权限需要动态获取, 核心权限必须满足. 标准流程:


如果用户点击,  不再提示, 则系统授权弹窗将不会弹出. 流程变为:


流程就这些, 让我们看看代码吧.

1. 权限

在AndroidManifest中, 添加两个权限, 录音修改音量.


<code class="xml"><span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--危险权限--></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.RECORD_AUDIO"</span>/></span>

    <span class="hljs-comment" style="color: rgb(136, 0, 0);"><!--一般权限--></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"android.permission.MODIFY_AUDIO_SETTINGS"</span>/></span></code>

危险权限必须要授权, 一般权限不需要.

检测权限类

<code class="java"><span class="hljs-comment" style="color: rgb(136, 0, 0);">/**
 * 检查权限的工具类
 * <p/>
 * Created by wangchenlong on 16/1/26.
 */</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">PermissionsChecker</span> </span>{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> Context mContext;

    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-title">PermissionsChecker</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(Context context)</span> </span>{
        mContext = context.getApplicationContext();
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 判断权限集合</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">boolean</span> <span class="hljs-title">lacksPermissions</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(String... permissions)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (String permission : permissions) {
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (lacksPermission(permission)) {
                <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>;
            }
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">false</span>;
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 判断是否缺少权限</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">boolean</span> <span class="hljs-title">lacksPermission</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(String permission)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> ContextCompat.checkSelfPermission(mContext, permission) ==
                PackageManager.PERMISSION_DENIED;
    }
}</code>

2. 首页

假设首页需要使用权限, 在页面显示前, 即onResume时, 检测权限,
如果缺少, 则进入权限获取页面; 接收返回值, 拒绝权限时, 直接关闭.

<code class="java"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">AppCompatActivity</span> </span>{

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> REQUEST_CODE = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 请求码</span>

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 所需的全部权限</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> String[] PERMISSIONS = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.MODIFY_AUDIO_SETTINGS
    };

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Bind</span>(R.id.main_t_toolbar) Toolbar mTToolbar;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> PermissionsChecker mPermissionsChecker; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 权限检测器</span>

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);

        setSupportActionBar(mTToolbar);

        mPermissionsChecker = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> PermissionsChecker(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onResume</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onResume();

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 缺少权限时, 进入权限配置页面</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            startPermissionsActivity();
        }
    }

    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">startPermissionsActivity</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
        PermissionsActivity.startActivityForResult(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, REQUEST_CODE, PERMISSIONS);
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onActivityResult</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> requestCode, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> resultCode, Intent data)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onActivityResult(requestCode, resultCode, data);
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 拒绝时, 关闭页面, 缺少主要权限, 无法运行</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            finish();
        }
    }
}</code>

核心权限必须满足, 如摄像应用, 摄像头权限就是必须的, 如果用户不予授权, 则直接关闭.

3. 授权页

授权页, 首先使用系统默认的授权页, 当用户拒绝时, 指导用户手动设置, 当用户再次操作失败后, 返回继续提示. 用户手动退出授权页时, 给使用页发送授权失败的通知.

<code class="java"><span class="hljs-comment" style="color: rgb(136, 0, 0);">/**
 * 权限获取页面
 * <p/>
 * Created by wangchenlong on 16/1/26.
 */</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">PermissionsActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">AppCompatActivity</span> </span>{

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> PERMISSIONS_GRANTED = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 权限授权</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> PERMISSIONS_DENIED = <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 权限拒绝</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> PERMISSION_REQUEST_CODE = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 系统权限管理页面的参数</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> String EXTRA_PERMISSIONS =
            <span class="hljs-string" style="color: rgb(0, 136, 0);">"me.chunyu.clwang.permission.extra_permission"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 权限参数</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">final</span> String PACKAGE_URL_SCHEME = <span class="hljs-string" style="color: rgb(0, 136, 0);">"package:"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 方案</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> PermissionsChecker mChecker; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 权限检测器</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">boolean</span> isRequireCheck; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 是否需要系统权限检测</span>

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 启动当前权限页面的公开接口</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">startActivityForResult</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(Activity activity, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> requestCode, String... permissions)</span> </span>{
        Intent intent = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Intent(activity, PermissionsActivity.class);
        intent.putExtra(EXTRA_PERMISSIONS, permissions);
        ActivityCompat.startActivityForResult(activity, intent, requestCode, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">null</span>);
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(@Nullable Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (getIntent() == <span class="hljs-keyword" style="color: rgb(0, 0, 136);">null</span> || !getIntent().hasExtra(EXTRA_PERMISSIONS)) {
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">throw</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> RuntimeException(<span class="hljs-string" style="color: rgb(0, 136, 0);">"PermissionsActivity需要使用静态startActivityForResult方法启动!"</span>);
        }
        setContentView(R.layout.activity_permissions);

        mChecker = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> PermissionsChecker(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);
        isRequireCheck = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>;
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onResume</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onResume();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (isRequireCheck) {
            String[] permissions = getPermissions();
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (mChecker.lacksPermissions(permissions)) {
                requestPermissions(permissions); <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 请求权限</span>
            } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
                allPermissionsGranted(); <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 全部权限都已获取</span>
            }
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
            isRequireCheck = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>;
        }
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 返回传递的权限参数</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> String[] getPermissions() {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 请求权限兼容低版本</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">requestPermissions</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(String... permissions)</span> </span>{
        ActivityCompat.requestPermissions(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, permissions, PERMISSION_REQUEST_CODE);
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 全部权限均已获取</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">allPermissionsGranted</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
        setResult(PERMISSIONS_GRANTED);
        finish();
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">/**
     * 用户权限处理,
     * 如果全部获取, 则直接过.
     * 如果权限缺失, 则提示Dialog.
     *
     * <span class="hljs-doctag">@param</span> requestCode  请求码
     * <span class="hljs-doctag">@param</span> permissions  权限
     * <span class="hljs-doctag">@param</span> grantResults 结果
     */</span>
    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onRequestPermissionsResult</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> requestCode, @NonNull String[] permissions, @NonNull <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span>[] grantResults)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
            isRequireCheck = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>;
            allPermissionsGranted();
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
            isRequireCheck = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">false</span>;
            showMissingPermissionDialog();
        }
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 含有全部的权限</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">boolean</span> <span class="hljs-title">hasAllPermissionsGranted</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(@NonNull <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span>[] grantResults)</span> </span>{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> grantResult : grantResults) {
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (grantResult == PackageManager.PERMISSION_DENIED) {
                <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">false</span>;
            }
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>;
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 显示缺失权限提示</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">showMissingPermissionDialog</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
        AlertDialog.Builder builder = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> AlertDialog.Builder(PermissionsActivity.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);
        builder.setTitle(R.string.help);
        builder.setMessage(R.string.string_help_text);

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 拒绝, 退出应用</span>
        builder.setNegativeButton(R.string.quit, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> DialogInterface.OnClickListener() {
            <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onClick</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(DialogInterface dialog, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> which)</span> </span>{
                setResult(PERMISSIONS_DENIED);
                finish();
            }
        });

        builder.setPositiveButton(R.string.settings, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> DialogInterface.OnClickListener() {
            <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onClick</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(DialogInterface dialog, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> which)</span> </span>{
                startAppSettings();
            }
        });

        builder.show();
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 启动应用的设置</span>
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">startAppSettings</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span> </span>{
        Intent intent = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
        startActivity(intent);
    }
}</code>

注意isRequireCheck参数的使用, 防止和系统提示框重叠.
系统授权提示: ActivityCompat.requestPermissions, ActivityCompat兼容低版本.

效果


关键部分就这些了, 动态权限授权虽然给程序员带来了一些麻烦, 但是对用户还是很有必要的, 我们也应该欢迎, 毕竟每个程序员都是半个产品经理.

危险权限列表

猜你喜欢

转载自blog.csdn.net/woainijinying/article/details/52694425