java 有没有办法以编程方式请求许可?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7517171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:19:16  来源:igfitidea点击:

Is there any way to ask permission programmatically?

javaandroidpermissions

提问by Adem

Is there any way to ask permission programmatically in android ? I don't want to add all permission to AndroidManifest.xml. So is there any dialog that asks for permission at runtime?

有没有办法在android中以编程方式请求许可?我不想将所有权限添加到 AndroidManifest.xml。那么有没有在运行时请求许可的对话框?

采纳答案by Daniel Novak

No. The user needs to be informed about the permissions while installing the application. Asking the user at runtime would be a security risk.

不可以。在安装应用程序时,用户需要被告知权限。在运行时询问用户会带来安全风险。

回答by Nate

Applications statically declare the permissions they require, and the Android system prompts the user for consent at the time the application is installed. Android has no mechanism for granting permissions dynamically (at run-time) because it complicates the user experience to the detriment of security.

应用程序静态声明它们需要的权限,Android 系统会在应用程序安装时提示用户同意。Android 没有动态授予权限的机制(在运行时),因为它会使用户体验复杂化,从而损害安全性。

回答by Machado

Not until now, but yes.

直到现在,但是是的

According to Google's new permission model introduced in Android M:

根据谷歌在Android M 中引入的新权限模型:

If an app running on the M Preview supports the new permissions model, the user does not have to grant any permissions when they install or upgrade the app. Instead, the app requests permissions as it needs them, and the system shows a dialog to the user asking for the permission.

如果在 M Preview 上运行的应用程序支持新的权限模型,则用户在安装或升级应用程序时无需授予任何权限。相反,应用程序会根据需要请求权限,系统会向用户显示一个对话框,请求权限。

Here's a summary of the key components of this new model:

以下是此新模型的关键组件的摘要:

  • Declaring Permissions: The app declares all the permissions it needs in the manifest, as in earlier Android platforms.

  • Permission Groups: Permissions are divided into permission groups, based on their functionality. For example, the CONTACTSpermission group contains permissions to read and write the user's contacts and profile information.

  • Limited Permissions Granted at Install Time: When the user installs or updates the app, the system grants the app all permissions listed in the manifest that fall under PROTECTION_NORMAL. For example, alarm clock and internet permissions fall under PROTECTION_NORMAL, so they are automatically granted at install time. For more information about how normal permissions are handled, see Normal Permissions. The system may also grant the app signature permissions, as described in System components and signature permissions. The user is not prompted to grant any permissions at install time.
  • User Grants Permissions at Run-Time: When the app requests a permission, the system shows a dialog to the user, then calls the app's callback function to notify it whether the user granted the permission. This permission model changes the way your app behaves for features that require permissions. Here's a summary of the development practices you should follow to adjust to this model:

  • Always Check for Permissions: When the app needs to perform any action that requires a permission, it should first check whether it has that permission already. If it does not, it requests to be granted that permission. You do not need to check for permissions that fall under PROTECTION_NORMAL.

  • Handle Lack of Permissions Gracefully: If the app is not granted an appropriate permission, it should handle the failure cleanly. For example, if the permission is just needed for an added feature, the app can disable that feature. If the permission is essential for the app to function, the app might disable all its functionality and inform the user that they need to grant that permission.

  • Permissions are Revocable: Users can revoke an app's permissions at any time. If a user turns off an app's permissions, the app is not notified. Once again, your app should verify that it has needed permissions before performing any restricted actions.

  • 声明权限:应用程序在清单中声明它需要的所有权限,就像在早期的 Android 平台中一样。

  • 权限组:权限根据其功能分为权限组。例如,CONTACTS权限组包含读取和写入用户联系人和个人资料信息的权限。

  • 安装时授予的有限权限:当用户安装或更新应用程序时,系统会授予应用程序清单中列出的属于PROTECTION_NORMAL. 例如,闹钟和互联网权限属于PROTECTION_NORMAL,因此它们会在安装时自动授予。有关如何处理普通权限的更多信息,请参阅普通权限。系统还可以授予应用程序签名权限,如系统组件和签名权限中所述。安装时不会提示用户授予任何权限。
  • 用户在运行时授予权限:当应用程序请求权限时,系统会向用户显示一个对话框,然后调用应用程序的回调函数来通知它用户是否授予了权限。此权限模型会更改您的应用程序对需要权限的功能的行为方式。以下是为适应此模型而应遵循的开发实践的摘要:

  • 始终检查权限:当应用程序需要执行任何需要权限的操作时,它应该首先检查它是否已经拥有该权限。如果没有,则请求授予该权限。您不需要检查属于 的权限PROTECTION_NORMAL

  • 优雅地处理缺乏权限:如果应用程序没有被授予适当的权限,它应该干净地处理失败。例如,如果添加的功能只需要权限,应用程序可以禁用该功能。如果权限对于应用程序的运行至关重要,则应用程序可能会禁用其所有功能并通知用户他们需要授予该权限。

  • 权限可撤销:用户可以随时撤销应用的权限。如果用户关闭应用程序的权限,则不会通知该应用程序。再一次,您的应用应在执行任何受限操作之前验证它是否具有所需的权限。

Source: https://developer.android.com/preview/features/runtime-permissions.html

来源:https: //developer.android.com/preview/features/runtime-permissions.html

回答by Eric

No.

不。

Answer here: get Android permission dynamiclly

在这里回答:动态获取 Android 权限

See the "Uses Permissions" section here: http://developer.android.com/guide/topics/security/security.html

请参阅此处的“使用权限”部分:http: //developer.android.com/guide/topics/security/security.html

回答by Prodigy

Android M introduced Runtime permissions, which everyone has been waiting for. Also the permissions are now categorized into NORMAL and DANGEROUS, where NORMAL permissions are granted by default and DANGEROUS permissions are requested when they are needed. Also DANGEROUS permissions can be revoked by the user at any time from the device's Settings menu.

Android M 引入了运行时权限,这是大家一直在等待的。此外,权限现在分为 NORMAL 和 DANGEROUS,其中默认授予 NORMAL 权限,并在需要时请求 DANGEROUS 权限。此外,用户可以随时从设备的“设置”菜单撤销“危险”权限。

回答by Oren

If I combine the answers from 'Piskvor' and from 'Hanno Binder', your app can check if the helper app is available (try to invoke it with an Intent), and if it is not there (the invocation fails), prompt the user to install it.

如果我将“Piskvor”和“Hanno Binder”的答案结合起来,您的应用程序可以检查辅助应用程序是否可用(尝试使用 Intent 调用它),如果不存在(调用失败),则提示用户安装它。

Look at the following, for example.

例如,请看以下内容。

how to download adobe reader programatically if not exists

如果不存在,如何以编程方式下载 adobe reader