在 Android 6(API 级别 23)及更高版本上,Android.Permission API 允许您在需要时请求使用某些常用系统功能(例如相机、麦克风或位置)的权限,而不是在应用程序启动时请求权限。您可以在需要时请求每个权限,并向用户显示一条消息以说明请求该权限的原因。
Android 对于权限使用的标准方法是在应用程序启动时向用户显示应用程序所需的权限列表,不解释为何使用这些权限或在何种背景下使用这些权限。这种方法可能会令人困惑,并且有些用户可能会拒绝权限(即使这些权限对于应用程序的操作是必需的)。
根据 Google 关于请求权限的准则,如果用户拒绝了一次权限请求,您应该向用户显示请求权限的原因,确保有机会让您再次提出请求。
可以使用 Permission.HasUserAuthorizedPermission 函数来检查用户是否已授予权限。如果没有,您可以再次请求权限,并解释应用程序需要该权限的原因。获得用户的批准后,请调用 Permission.RequestUserPermission 再次请求权限。调用此函数时,Android 会打开系统权限对话框,以允许用户批准(或拒绝)该权限。
如果用户仍然拒绝权限请求,则应禁用应用程序中的相关功能,或者,如果应用程序在没有该权限的情况下无法运行,请通知用户。请注意,如果用户先前在系统权限对话框中选中了“Do not ask me again”选项,则 RequestUserPermission()
不会打开系统对话框。在这种情况下,用户必须进入应用程序权限设置并手动打开权限。
可将 unityplayer.SkipPermissionsDialog
元数据添加到 Android 清单,以禁止通常在应用程序需要从用户获取受保护功能的访问权限时显示的权限对话框。如果这样做,必须在适当的时间调用 Permission.RequestUserPermission 来获取对每个受保护功能的访问权限。请参阅 Android 清单以了解更多信息。
有关请求 Android 权限的更多信息,请参阅 Android 开发者指南中的应用权限最佳实践。
以下示例代码说明了如何检查是否已授予特定权限,并在用户拒绝权限的情况下显示对话框:
using UnityEngine;
# if PLATFORM_ANDROID
using UnityEngine.Android;
# endif
public class MicrophoneTest : MonoBehaviour
{
GameObject dialog = null;
void Start ()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
Permission.RequestUserPermission(Permission.Microphone);
dialog = new GameObject();
}
#endif
}
void OnGUI ()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// 用户拒绝了使用麦克风的权限。
//显示一条包含 Yes/No 按钮的消息,说明需要该权限的原因。
//如果用户选择 yes,则再次提出请求
// 在此处显示对话框。
dialog.AddComponent<PermissionsRationaleDialog>();
return;
}
else if (dialog != null)
{
Destroy(dialog);
}
#endif
//现在可以用麦克风执行所需的操作了
}
}
using UnityEngine;
# if PLATFORM_ANDROID
using UnityEngine.Android;
# endif
public class PermissionsRationaleDialog : MonoBehaviour
{
const int kDialogWidth = 300;
const int kDialogHeight = 100;
private bool windowOpen = true;
void DoMyWindow(int windowID)
{
GUI.Label(new Rect(10, 20, kDialogWidth - 20, kDialogHeight - 50), "Please let me use the microphone.");
GUI.Button(new Rect(10, kDialogHeight - 30, 100, 20),"No");
if (GUI.Button(new Rect(kDialogWidth - 110, kDialogHeight - 30, 100, 20), "Yes"))
{
#if PLATFORM_ANDROID
Permission.RequestUserPermission(Permission.Microphone);
#endif
windowOpen = false;
}
}
void OnGUI ()
{
if (windowOpen)
{
Rect rect = new Rect((Screen.width / 2) - (kDialogWidth / 2), (Screen.height / 2) - (kDialogHeight / 2), kDialogWidth, kDialogHeight);
GUI.ModalWindow(0, rect, DoMyWindow, "Permissions Request Dialog");
}
}
}
注意:应将 MicrophoneTest
(而不是 PermissionsRationaleDialog
)作为组件添加到游戏对象。MicrophoneTest
会自动创建一个游戏对象,并在需要显示对话框时添加 PermissionsRationaleDialog
。另请注意,该应用程序必须使用 Unity 的 Microphone 类添加麦克风权限。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.