Android Management API 的委派范围似乎没有被 Companion 应用程序获取

Android Management API's Delegation Scopes do not seem to be acquired by Companion app

Android Management API reference 之后,我们配置了 Android 配套应用,使其能够:启用系统应用、隐藏和取消隐藏包。

我们在策略配置中添加了以下信息:

{
  '
  '
  '
  "applications": [
    {
      "packageName": "com.domain.app",
      "installType": "REQUIRED_FOR_SETUP",
      "defaultPermissionPolicy": "GRANT",
      "delegatedScopes": [
        "ENABLE_SYSTEM_APP",
        "PACKAGE_ACCESS"
      ]
    }
  ],
  '
  '
  '
}

然后,在我们的 Android 配套应用中,我们根据 Google 文档 here and here 添加了以下代码行:

DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName admin = new ComponentName(mContext, DeviceAdminReceiver.class);

// Iterate through system application package names list
for (String packageName : systemAppsList) {
  if (packageName != null && !packageName.equals("")) {
    try {
      // Re-enable a system app that was disabled by default when the user was initialized
      dpm.enableSystemApp(admin, packageName);

      // Unhide a package (it could be any app : system, managed, etc...)
      dpm.setApplicationHidden(admin, packageName, false);
    } catch (SecurityException e) {
      e.printStackTrace();
      Log.e(TAG, e.getMessage());
    }
  }
}

我们希望 Android 启动器显示启用的系统应用程序,但 Android 捕获 SecurityException 并打印以下错误日志:

No active admin ComponentInfo{com.domain.app/android.app.admin.DeviceAdminReceiver}

你知道哪里出了问题吗?

在您的方法调用中,如果您正在使用委派作用域,请将管理参数设置为 null,如 Android Documentation

中所述

ComponentName: Which DeviceAdminReceiver this request is associated with, or null if the caller is a package access delegate. This value must never be null.

有点令人困惑,如果使用委托范围并且在同一时间之后,admin 参数可以设置为 null ...此值绝不能为 null,(很棒 Google)