gpt4 book ai didi

android - 统一 + 安卓 : How to ask for multiple permissions?

转载 作者:行者123 更新时间:2023-11-30 04:58:26 25 4
gpt4 key购买 nike

我正在使用目标平台 Android 开发一个 Unity(版本 2019.1.11)项目,我需要拥有以下权限才能使我的应用运行:

android.permission.CAMERA
android.permission.RECORD_AUDIO
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_PHONE_STATE

Unity 文档指定 this请求Android权限的方式。我的目标是在应用程序启动期间对所有必需的权限进行初步检查,如下所示:

private void AskPermissions()
{
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
Permission.RequestUserPermission(Permission.Microphone);
}
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
}
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
if (!Permission.HasUserAuthorizedPermission("android.permission.READ_PHONE_STATE"))
{
Permission.RequestUserPermission("android.permission.READ_PHONE_STATE");
}
#endif
}

但是,这不起作用:该应用程序仅显示第一个未授权权限的对话框,而不显示之后正在检查的未授权权限的对话框。

如何确保始终检查所有权限?

最佳答案

似乎 Permission.RequestUserPermission 以某种方式异步工作,并且在已经显示对话框时不会显示对话框 - 因此在找到第一个未授权权限后简单地跳过所有其他权限。
我可以绕过这样的问题:

private IEnumerator AskForPermissions()
{
#if UNITY_ANDROID
List<bool> permissions = new List<bool>() { false, false, false, false };
List<bool> permissionsAsked = new List<bool>() { false, false, false, false };
List<Action> actions = new List<Action>()
{
new Action(() => {
permissions[0] = Permission.HasUserAuthorizedPermission(Permission.Microphone);
if (!permissions[0] && !permissionsAsked[0])
{
Permission.RequestUserPermission(Permission.Microphone);
permissionsAsked[0] = true;
return;
}
}),
new Action(() => {
permissions[1] = Permission.HasUserAuthorizedPermission(Permission.Camera);
if (!permissions[1] && !permissionsAsked[1])
{
Permission.RequestUserPermission(Permission.Camera);
permissionsAsked[1] = true;
return;
}
}),
new Action(() => {
permissions[2] = Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite);
if (!permissions[2] && !permissionsAsked[2])
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
permissionsAsked[2] = true;
return;
}
}),
new Action(() => {
permissions[3] = ermission.HasUserAuthorizedPermission("android.permission.READ_PHONE_STATE");
if (!permissions[3] && !permissionsAsked[3])
{
Permission.RequestUserPermission("android.permission.READ_PHONE_STATE");
permissionsAsked[3] = true;
return;
}
})
};
for(int i = 0; i < permissionsAsked.Count; )
{
actions[i].Invoke();
if(permissions[i])
{
++i;
}
yield return new WaitForEndOfFrame();
}
#endif
}

关于android - 统一 + 安卓 : How to ask for multiple permissions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58728596/

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