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兼容低版本.

效果


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

危险权限列表

《餐馆点餐管理系统——基于Java和MySQL的课程设计解析》 在信息技术日益发达的今天,餐饮行业的数字化管理已经成为一种趋势。本次课程设计的主题是“餐馆点餐管理系统”,它结合了编程语言Java和数据库管理系统MySQL,旨在帮助初学者理解如何构建一个实际的、具有基本功能的餐饮管理软件。下面,我们将深入探讨这个系统的实现细节及其所涉及的关键知识点。 我们要关注的是数据库设计。在“res_db.sql”文件中,我们可以看到数据库的结构,可能包括菜品表、订单表、顾客信息表等。在MySQL中,我们需要创建这些表格并定义相应的字段,如菜品ID、名称、价格、库存等。此外,还要设置主键、外键来保证数据的一致性和完整性。例如,菜品ID作为主键,确保每个菜品的唯一性;订单表中的顾客ID和菜品ID则作为外键,与顾客信息表和菜品表关联,形成数据间的联系。 接下来,我们来看Java部分。在这个系统中,Java主要负责前端界面的展示和后端逻辑的处理。使用Java Swing或JavaFX库可以创建用户友好的图形用户界面(GUI),让顾客能够方便地浏览菜单、下单。同时,Java还负责与MySQL数据库进行交互,通过JDBC(Java Database Connectivity)API实现数据的增删查改操作。在程序中,我们需要编写SQL语句,比如INSERT用于添加新的菜品信息,SELECT用于查询所有菜品,UPDATE用于更新菜品的价格,DELETE用于删除不再提供的菜品。 在系统设计中,我们还需要考虑一些关键功能的实现。例如,“新增菜品和价格”的功能,需要用户输入菜品信息,然后通过Java程序将这些信息存储到数据库中。在显示所有菜品的功能上,程序需要从数据库获取所有菜品数据,然后在界面上动态生成列表或者表格展示。同时,为了提高用户体验,可能还需要实现搜索和排序功能,允许用户根据菜品名称或价格进行筛选。 另外,安全性也是系统设计的重要一环。在连接数据库时,要避免SQL注入攻击,可以通过预编译的PreparedStatement对象来执行SQL命令。对于用户输入的数据,需要进行验证和过滤,防止非法字符和异常值。 这个“餐馆点餐管理系统”项目涵盖了Java编程、数据库设计与管理、用户界面设计等多个方面,是一个很好的学习实践平台。通过这个项目,初学者不仅可以提升编程技能,还能对数据库管理和软件工程有更深入的理解。在实际开发过程中,还会遇到调试、测试、优化等挑战,这些都是成长为专业开发者不可或缺的经验积累
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值