gpt4 book ai didi

android - 在应用程序安装期间获取针对 API 26 的所有权限

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:43 25 4
gpt4 key购买 nike

在 Android API 版本 22 及之前,系统将在安装时获得所有权限。从 API 23(Marshmallow) 开始,系统会在运行时(当应用程序实际尝试使用某个功能时)请求许可。由于某些原因,这并不是很好。

  • 开发人员必须编写 2 个不同的(1 - 如果用户授予权限,2.如果用户拒绝许可)代码一直流。

  • 在请求权限时,Android 可以完全控制权限对话框中显示的文本,并要求用户按"is"或“否”。有时,权限对话框中显示的文本不合适。

  • 当我们请求多个权限时,如果其中一个权限被拒绝,应用程序会因 Android 代码错误而崩溃。这是 API 23、24 和 25 的问题。我认为它已在 26 中修复。

我们的应用程序面临的问题,

示例 1:出于安全原因,我们试图获取设备 ID,但当我们请求权限时,Android 会显示类似“允许 MyApp 管理电话”的消息。我们与电话无关,消息不适合访问设备ID。这会降低应用程序的用户注册(注册),营销团队非常担心。

示例 2:我们有很多 Web View ,我们尝试尽可能多地使用离线存储和缓存以获得更好的用户体验,但用户拒绝权限,因为 Android 权限对话框显示“Allow MyApp to访问存储文件'。

所以,问题是,是否可以在较新的 API 版本(23 及更高版本)上自行安装应用程序(如 API v22 及之前版本)而不是在运行时请求所有权限?

最佳答案

首先,请求权限运行时是最佳实践,这是谷歌的解释

In some circumstances, you might want to help the user understand why your app needs a permission. For example, if a user launches a photography app, the user probably won't be surprised that the app asks for permission to use the camera, but the user might not understand why the app wants access to the user's location or contacts. Before you request a permission, you should consider providing an explanation to the user. Keep in mind that you don't want to overwhelm the user with explanations; if you provide too many explanations, the user might find the app frustrating and remove it.

One approach you might use is to provide an explanation only if the user has already turned down that permission request. If a user keeps trying to use functionality that requires a permission, but keeps turning down the permission request, that probably shows that the user doesn't understand why the app needs the permission to provide that functionality. In a situation like that, it's probably a good idea to show an explanation.

To help find situations where the user might need an explanation, Android provides a utiltity method, shouldShowRequestPermissionRationale(). This method returns true if the app has requested this permission previously and the user denied the request.

Note: If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if a device policy prohibits the app from having that permission.

因此,在您的情况下,如果您仔细阅读文档,您可以在安装时请求所有权限一次。在你的build.gradle文件(App)中,将targetSdkVersion设置为低于23的api级别。例如:

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "vn.yourpackage.name"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

关于android - 在应用程序安装期间获取针对 API 26 的所有权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45134801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com