Android:检查是否必须询问运行时权限 [英] Android: Check whether runtime permissions must be asked or not

查看:349
本文介绍了Android:检查是否必须询问运行时权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查特定应用程序是否需要在运行时处理Android Marshmallow运行时权限.

I would like to check whether a particular app need to handle Android Marshmallow runtime permissions in runtime or not.

以下假设是否正确?

/**
 * Checks whether runtime permissions must be handled or not.
 *
 * @param context Application context.
 * @return Handle runtime permissions or not.
 */
public static boolean shouldCheckRuntimePermissions(Context context) {
    return
            isApplicationWithMarshmallowTargetSdkVersion(context) &&
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

/**
 * Checks whether the app is compiled with targetSdkVersion Marshmallow or above.
 *
 * @param context Application context.
 * @return Application targetSdkVersion above 23 or not.
 */
public static boolean isApplicationWithMarshmallowTargetSdkVersion(Context context) {
    return
            context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M;
}

这是我详细说明的表格,例如,需要ACCESS_COARSE_LOCATION权限才能向具有Android Marshmallow运行时权限的用户询问.

Here is the table I have elaborated where for example ACCESS_COARSE_LOCATION permissions were needed to ask to the user with Android Marshmallow runtime permissions.

====================
target SDK |<23| 23|
====================
       |<23| X | X |
       ------------| 
OS SDK |23 | X | V |
====================   

我知道支持库 helpers 的存在,但我想避免他们在这种情况下.

I know the existence of Support library helpers, but I want to avoid them for this particular case.

推荐答案

@LuisMiguelSierra是正确的,我在问题末尾发布的链接中得到了答案,但我完全没有意识到.这是回答我的问题并确认我的假设和方法正确的报价:

@LuisMiguelSierra was right, I had the answer in the link I posted at the end of my question and I didn't realize at all. Here is the quote that answers my question and confirms my assumption and methods were right:

在所有版本的Android上,您的应用都需要同时声明正常 以及它在其应用清单中所需的危险权限,例如 声明权限中所述.但是,那的效果 声明因系统版本和您的系统而异 应用的目标SDK级别:

On all versions of Android, your app needs to declare both the normal and the dangerous permissions it needs in its app manifest, as described in Declaring Permissions. However, the effect of that declaration is different depending on the system version and your app's target SDK level:

  • 如果设备运行的是Android 5.1或更低版本,或者您的应用的目标SDK为22或更低版本:如果您在
    中列出了危险权限 清单,则用户在安装
    时必须授予权限 应用程序;如果他们不授予权限,则系统不会安装 该应用程序.
  • 如果设备运行的是Android 6.0或更高版本,并且您的应用的目标SDK为23或更高版本:该应用必须在以下位置列出权限: 清单,并且必须请求每个危险的许可 应用程序运行时需要.用户可以授予或拒绝每个 权限,并且该应用可以在有限的条件下继续运行
    功能,即使用户拒绝许可请求也是如此.
  • If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your
    manifest, the user has to grant the permission when they install the
    app; if they do not grant the permission, the system does not install the app at all.
  • If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited
    capabilities even if the user denies a permission request.

因此,可以确定何时提出权限的这种假设:

So this assumption of when to ask permissions is confirmed:

====================
target SDK |<23| 23|
====================
       |<23| X | X |
       ------------| 
OS SDK |23 | X | V |
====================

这篇关于Android:检查是否必须询问运行时权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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