根据API级别请求权限 [英] Request permissions based on API level

查看:115
本文介绍了根据API级别请求权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Android 6.0(API级别23)开始,我们可以在运行时询问权限.但是,根据文档,所有权限仍然需要在AndroidManifest.xml中定义,因此在23以下的API中,这些权限将在安装应用程序之前授予.

我只想在运行时请求ACCESS_FINE_LOCATION权限-由于这是一个敏感的权限,因此在安装前无任何上下文地请求它会导致下载量减少.

我将我的应用程序定位为API级别11+,所以我想知道是否有可能不要求在较旧的API中使用ACCESS_FINE_LOCATION权限(即不在AndroidManifest中针对23岁以上的API列出它),而仅请求适用于API 23 +.

更新

为澄清起见,我想知道是否可以执行以下操作:

AndroidManifest.xml

IF API_LEVEL>=23: {
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
}
ELSE:
{
}

解决方案

  • 不能请求清单中未声明的权限.
  • 不能在运行时更改您的清单.

您可以做的是制作具有声明权限的 1种口味定位API 23 + ,并制作另一种没有权限定位API 23-的口味.

Beginning in Android 6.0 (API level 23), we can ask permissions at run time. However, according to the docs, all permissions still need to be defined in AndroidManifest.xml so in APIs lower than 23, these permissions will be granted prior to installing the app.

I want to request ACCESS_FINE_LOCATION permission only at runtime - since this is a sensitive permission, requesting it prior to installation without any context will cause a decrease in downloads.

I am targeting my app to API levels 11+, so I wonder whether it is possible not to ask for the ACCESS_FINE_LOCATION permission in older APIs (i.e. not list it in AndroidManifest for APIs older than 23), and only request it for APIs 23+.

Update

For clarification, I want to know whether it is possible to do the following:

AndroidManifest.xml

IF API_LEVEL>=23: {
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
}
ELSE:
{
}

解决方案

  • You can't request a Permission which is not declared in Manifest.
  • You can't change your Manifest at runtime.

What you can do is to make 1 flavor targeting API 23+ with permission declared and another flavor without the permission targeting API 23-.

这篇关于根据API级别请求权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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