Android Permission

本文深入解析了Android中的正常权限和危险权限,阐述了不同目标API级别下权限的处理方式,包括安装时授予、运行时请求及外部存储权限的变化,并探讨了在特定场景下无需权限的情况。

Normal permission and Dangerous permission

• Normal permissions do not directly risk the user’s privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
• Dangerous permissions can give the app access to the user’s confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, from Android 6.0, the user has to explicitly give approval to your app.

If target API less than 23:

• No matter what’s version OS is, all Permission in Manifest will be granted by System during installation. And users can see that.
• No matter what’s version OS is, deny the permission in Android settings screen can’t make any change. The permission is still granted. (For OS less than M, there might be no deny in settings screen.)

If target API higher or equal 23:

• Dangerous permissions should be granted during runtime by users. And they should also be declared in Manifest.
• If OS version higher or equal to M, users can see all the dangerous permission. But all of them are grey/off/denied by default. We should promote them and let users grant.
• Both ContextCompat in support library and native Activity have checkSelfPermission(),requestPermissions() and onRequestPermissionsResult() APIs to do runtime requesting.
• Only the permissions declared in manifest could be requested run time.
• Runtime permission is an additional requirement for targeting API higher than 23. It doesn’t enable developer more abilities but more restriction.
• If OS version lower than M, All dangerous permissions will be granted by system during installation.

Merged Manifest

In Android studio, there’s a tap at the bottom of Manifest file named “Merged Manifest” which could be used to check which libraries the permission is from.
Permission merged from libraries could be removed by tools:node=”remove” in main manifest.
could request permission specific for target API higher than or equal to 23. And for the situation that depending library targets API higher than 23 but main App targets API lower than 23, the dangerous permissions declared in depending library will be tagged as “<users-permission-sdk-23 ” automatically during merging.

External Storage Permission

  • Before API 18, developers need put READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in manifest to read/write to application’s data folder.
  • After API 18, no permission needed to read/write to application’s data folder.

Some Scenarios No Permission Needed

  • To open/read file with Intent.ACTION_OPEN_DOCUMENT, no permission needed.
  • To open camera with ACTION_IMAGE_CAPTURE, no permission needed.
<think>我们正在讨论在UE5中使用AndroidPermission模块的方法。根据之前的对话,我们知道PermissionX不适用于UE5,但UE5有自己的AndroidPermission模块来处理Android权限。 因此,我将详细介绍如何在UE5中使用AndroidPermission模块,包括配置、请求权限和处理回调。 步骤: 1. 添加模块依赖 2.AndroidManifest.xml中声明权限(通过UE5项目设置) 3. 使用C++或蓝图请求权限 4. 处理权限回调 同时,我会在适当的地方添加引用标识,并在最后生成相关问题。 </think>在 Unreal Engine 5 (UE5) 中使用 `AndroidPermission` 模块分为以下几个关键步骤,所有操作均使用 UE5 原生 API 实现: --- ### 1. **添加模块依赖** 在项目配置文件 `YourProject.Build.cs` 中添加模块依赖: ```cpp PublicDependencyModuleNames.AddRange(new string[] { "Core", "AndroidPermission" // 添加权限模块 }); ``` [^1] --- ### 2. **配置 Android 权限** 在 **项目设置 → 平台 → Android** 中: 1. `Extra Permissions` 字段添加所需权限: ```plaintext android.permission.CAMERA android.permission.READ_EXTERNAL_STORAGE ``` 2. 勾选 `Enable Runtime Permissions` 启用动态权限 [^2] --- ### 3. **C++ 实现权限请求** #### 权限检查: ```cpp #include "AndroidPermissionFunctionLibrary.h" #include "AndroidPermissionCallbackProxy.h" // 检查单个权限 bool HasCameraPermission = UAndroidPermissionFunctionLibrary::CheckPermission("android.permission.CAMERA"); // 检查多个权限 TArray<FString> Permissions = { "android.permission.CAMERA", "android.permission.READ_EXTERNAL_STORAGE" }; bool AllGranted = UAndroidPermissionFunctionLibrary::CheckPermissions(Permissions); ``` #### 请求权限: ```cpp // 声明回调代理对象(必须持久化!) UAndroidPermissionCallbackProxy* PermissionProxy; void AYourClass::RequestPermissions() { TArray<FString> PermissionsToRequest = { "android.permission.CAMERA", "android.permission.READ_EXTERNAL_STORAGE" }; // 请求权限 PermissionProxy = UAndroidPermissionFunctionLibrary::AcquirePermissions(PermissionsToRequest); // 绑定回调事件 PermissionProxy->OnPermissionsGrantedDelegate.AddDynamic(this, &AYourClass::OnPermissionsGranted); PermissionProxy->OnPermissionsDeniedDelegate.AddDynamic(this, &AYourClass::OnPermissionsDenied); } // 权限授予回调 void AYourClass::OnPermissionsGranted(const TArray<FString>& Permissions) { UE_LOG(LogTemp, Warning, TEXT("%d permissions granted"), Permissions.Num()); } // 权限拒绝回调 void AYourClass::OnPermissionsDenied(const TArray<FString>& Permissions) { UE_LOG(LogTemp, Error, TEXT("%d permissions denied"), Permissions.Num()); } ``` [^3] --- ### 4. **蓝图实现(可视化操作)** 1. 在蓝图中调用 `Check Permission` 节点检查权限 2. 使用 `Acquire Permissions` 节点请求权限 ![蓝图权限请求示例](https://docs.unrealengine.com/5.3/Images/developer-reference/platforms/android/android-permission-flow.webp) 3. 通过 `On Permissions Granted` 事件处理结果 [^4] --- ### 关键注意事项 1. **代理对象生命周期** `UAndroidPermissionCallbackProxy` 必须作为类成员变量保存,否则会被垃圾回收导致回调失效: ```cpp UPROPERTY() UAndroidPermissionCallbackProxy* PermissionProxy; // 添加UPROPERTY防止回收 ``` 2. **权限拒绝处理** 当用户拒绝权限时,应引导至系统设置页: ```cpp FPlatformMisc::OsExecute(TEXT(""), TEXT("package:com.YourCompany.YourGame")); ``` 3. **Android 12+ 适配** 对于精确定位等敏感权限,需在 `AndroidManifest.xml` 额外声明: ```xml <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值