Android-处理必须授予应用才能正常工作的危险许可的正确方法? [英] Android - Correct way to handle a dangerous permission that must be granted for app to work?

查看:77
本文介绍了Android-处理必须授予应用才能正常工作的危险许可的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我们要处理的数据量很大,我的应用程序需要访问存储-只是读取一些OBB数据.根据定义,这被android归类为危险"权限,因此,从Android 6.0开始,我们需要在应用开始时先请求它,然后我们才能进行任何操作.

My app requires access to the storage - just to read some OBB data - due to the size of the data we're dealing with. By definition this is classed as a 'dangerous' permission by android, and rightly so, so as of Android 6.0 we need to request it at the app's start before we can do anything.

问题是,当应用程序从根本上要求应用程序执行除主菜单之外的任何操作时,用户都可以拒绝我们的许可.

The issue is that the user can deny us that permission, when the app fundamentally requires it to do anything past the main menu.

处理用户拒绝在运行时授予危险"权限而没有该功能的应用程序几乎没有功能的正确方法是什么?

What is the correct way to deal with the situation where a user refuses to grant 'dangerous' permission at runtime, and an app is practically featureless without it?

要弄清,我知道我需要执行此操作的代码以及所涉及的回调.不需要代码示例.我需要知道如果不断地拒绝将使我的应用瘫痪,我该怎么办.错误然后杀死该应用程序?当用户尝试执行任何操作时,显示带有消息的主菜单?可能还值得注意的是,这是一个本机应用程序,因此C ++会回调Java来完成此操作.

To clarify I know the code i need to do this and the callbacks involved. Code samples are not required. I need to know what i should do if constant denial will cripple my app to the usefulness of a rock. Error then kill the app? Show main menu with a message when the user tries to do anything? It may also be worth noting that this is a native app, so the C++ is calling back to the java to do this.

推荐答案

其他答案中未描述的关键位是在用户拒绝提供权限之后,建议的方法是显示一个原理对话框,解释为什么您的许可至关重要.有一个兼容性帮助程序可以跟踪此内部状态:

The crucial bit not yet described in the other answers is after a user has declined to provide the permission, the recommended approach is to show a rationale dialog explaining why your permission is essential. There is a compatibility helper that tracks this internal state:

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        new AlertDialog.Builder(this)
                .setTitle(R.string.title_my_permission)
                .setMessage(R.string.text_my_permission)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Prompt the user once explanation has been shown
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_TAG);
                    }
                })
                .create()
                .show();
    } else {
        // probably first time, request permission
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_TAG);
    }

但是,您应该注意,用户始终处于控制之中,并且可能会决定永远不授予您的应用所需的权限,因此,您需要在尽可能多的语言环境中使用大量文本,才能说服他们:-)

You should note however that the user is always in control and may decide never to grant your app the permission it needs, so you need a good bit of text in as many locales as you can manage to convince them :-)

更糟糕的是,他们可以在原始请求对话框中选择不再询问".在这种情况下, ActivityCompat.shouldShowRequestPermissionRationale 也会返回false.如果确实需要,可以使用第三个对话框警告用户.但是,在这种情况下,用户自己必须在Android设置活动中手动启用权限.您可以帮助他们使用意图查找此活动,但是一旦他们选择了不再询问",他们就必须手动检查权限.

Worse, they can select "never ask again" in the original request dialog. In that case, ActivityCompat.shouldShowRequestPermissionRationale returns false also. If you really want to, you can warn the user using a third dialog. However, in that case, the user themselves must manually enable the permission inside the Android settings activity. You can help them find this activity using an intent but they must manually check the permission once they have selected "never ask again".

这篇关于Android-处理必须授予应用才能正常工作的危险许可的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆