我们如何在 Android M 的运行时权限中区分 Never-Ask 和 Stop-Ask? [英] How Do We Distinguish Never-Asked From Stop-Asking in Android M's Runtime Permissions?

查看:16
本文介绍了我们如何在 Android M 的运行时权限中区分 Never-Ask 和 Stop-Ask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当谈到 M Developer Preview 运行时权限时,根据 Google:

When it comes to the M Developer Preview runtime permissions, according to Google:

  1. 如果您以前从未请求过某个权限,请直接请求

  1. If you have never asked for a certain permission before, just ask for it

如果你之前问过,用户说不",然后用户尝试做一些需要被拒绝权限的事情,你应该提示用户解释你为什么需要权限,然后再继续再次请求权限

If you asked before, and the user said "no", and the user then tries doing something that needs the rejected permission, you should prompt the user to explain why you need the permission, before you go on to request the permission again

如果你之前问过几次,而用户说不,停止询问"(通过运行时权限对话框上的复选框),你应该停止打扰(例如,禁用 UI需要权限)

If you asked a couple of times before, and the user has said "no, and stop asking" (via the checkbox on the runtime permission dialog), you should just stop bothering (e.g., disable the UI that requires the permission)

然而,我们只有一种方法,shouldShowRequestPermissionRationale(),返回一个boolean,我们有三种状态.我们需要一种方法来区分从不询问状态和停止询问状态,因为我们从 shouldShowRequestPermissionRationale() 得到 false 两者.

However, we only have one method, shouldShowRequestPermissionRationale(), returning a boolean, and we have three states. We need a way to distinguish the never-asked state from the stop-asking state, as we get false from shouldShowRequestPermissionRationale() for both.

对于首次运行应用程序时请求的权限,这不是大问题.有很多方法可以确定这可能是您的应用程序的第一次运行(例如,SharedPreferences 中的 boolean 值),因此您假设它是第一次运行对于您的应用,您处于从未被询问的状态.

For permissions being requested on first run of the app, this is not a big problem. There are plenty of recipes for determining that this is probably the first run of your app (e.g., boolean value in SharedPreferences), and so you assume that if it's the first run of your app, you're in the never-asked state.

但是,运行时权限的部分愿景是您可能不会预先要求所有权限.与边缘功能相关的权限,您可能只会在稍后当用户点击需要该权限的内容时才要求.在这里,应用程序可能已经运行了很多次,几个月了,然后我们突然需要请求另一个权限.

However, part of the vision of runtime permissions is that you might not ask for all of them up front. Permissions tied to fringe features you might only ask for later on, when the user taps on something that requires that permission. Here, the app may have been run many times, for months, before we all of a sudden need to request another permission.

在这些情况下,我们是否应该跟踪我们是否自己请求许可?或者我遗漏了 Android M API 中的某些内容,可以告诉我们之前是否询问过?

In those cases, are we supposed to track whether or not we asked for the permission ourselves? Or is there something in the Android M API that I am missing that tells us whether we asked before or not?

推荐答案

根据当前示例:https://github.com/googlesamples/android-RuntimePermissions/blob/master/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java#L195

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
        int[] grantResults) {
    if (requestCode == REQUEST_CAMERA) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            doThing();
            //STORE FALSE IN SHAREDPREFERENCES
        } else {
            //STORE TRUE IN SHAREDPREFERENCES
        }
    }

在 SharedPreferences 中存储一个布尔值,键作为您的权限代码和值,如上所示,以指示该首选项之前是否已被拒绝.

Store a boolean in SharedPreferences with key as your permission code and value as indicated above, to indicate whether that preference has been denied before.

遗憾的是,您可能无法检查在您的应用运行时已被接受但后来被拒绝的首选项.最终规范不可用,但您的应用有可能在下次启动之前重新启动或获得模拟值.

Sadly, you probably can't check against a preference that has been accepted and later denied while your app is running. The final spec is not available, but there's a chance that your app either gets restarted or gets mock values until the next launch.

这篇关于我们如何在 Android M 的运行时权限中区分 Never-Ask 和 Stop-Ask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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