You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android14无法限制相机使用:Device Admin权限失效问题求助

问题原因与解决方案

核心问题

你遇到的SecurityException本质是旧版Device Admin框架在Android 14中已被彻底弃用——即使isAdminActive()返回true,普通设备管理员应用也不再被授予MANAGE_DEVICE_POLICY_CAMERA这类设备策略权限,该权限仅对Device Owner或Profile Owner应用开放。

代码异常复现

调用以下代码时触发权限错误:

policyManager.setCameraDisabled(componentName, true);

异常信息:

java.lang.SecurityException: Caller does not have the required permissions for this user. Permission required: android.permission.MANAGE_DEVICE_POLICY_CAMERA.

解决步骤

  1. 迁移至Device Owner/Profile Owner模式

    • 旧Device Admin已不再支持设备策略控制,必须将应用设置为Device Owner(适用于单用户设备)或Profile Owner(适用于工作资料场景)。
    • 可通过ADB命令快速设置测试:
      dpm set-device-owner com.your.package/.YourDeviceAdminReceiver
      
    • 正式部署需通过企业移动管理渠道配置。
  2. 调整Manifest权限与组件配置

    • 确保在AndroidManifest.xml中声明权限:
      <uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_CAMERA" />
      
    • 确认DeviceAdminReceiver的meta-data和intent-filter配置正确,需包含android.app.device_admin权限相关声明。
  3. 代码校验逻辑更新

    • 调用设备策略接口前,需先验证应用是否为Device/Profile Owner,而非仅依赖isAdminActive():
      DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
      ComponentName adminComponent = new ComponentName(this, YourDeviceAdminReceiver.class);
      if (dpm.isDeviceOwnerApp(getPackageName()) || dpm.isProfileOwnerApp(getPackageName())) {
          dpm.setCameraDisabled(adminComponent, true);
      } else {
          // 提示需设置为设备所有者
      }
      

内容的提问来源于stack exchange,提问作者charles kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 19:38:17