典型的管理任务

EMM 可以在其 Android 企业解决方案中实现一系列典型的管理任务,例如本指南中显示的任务。示例代码使用适用于 Java 的 Google Play EMM API 客户端库®。

查找用户并获取用户详细信息

许多 Google Play EMM API 都需要 userId(以及 enterpriseId)来执行一些与用户相关的任务,如安装应用、获取使用权和获取许可。某些调用需要使用 user 对象。

如果组织使用 Google Play 企业版帐号,您必须在用户电子邮件地址和这些帐号的用户 ID 之间建立映射。通过 Google 帐号,您可以使用 UsersListResponse

查找用户

此代码示例可获取与 Google 帐号关联的用户 ID。不适用于使用 Google Play 企业版帐号的组织。

此示例假定您已将 UserUsersListResponse 模型导入到您的代码中。如需获取特定用户帐号,请通过将 enterpriseId 和电子邮件地址传递给 UsersListResponse 来获取其 userId

public UsersListResponse list(String enterpriseId, String email) throws
   IOException {
    return androidEnterprise
        .users()
        .list(enterpriseId, email)
        .execute();
}

获取用户详细信息

有了 userId 后,您可以检索其他详细信息。请参阅 Users.Get

public User get(String enterpriseId, String userId) throws IOException {
    return androidEnterprise
        .users()
        .get(enterpriseId, userId)
        .execute();
}

向用户授予应用访问权限

您可以使用 Google Play EMM API 来控制用户可以从 Google Play 企业版商店访问哪些应用。对应用的访问权限包括搜索、查看、安装和更新应用。有三种不同的访问权限级别:

  • 仅限允许的应用:用户只能访问特定应用。
  • 所有已获批准的应用:用户可以访问已批准用于企业的所有应用。
  • 所有应用:用户可以访问 Google Play 商店中公开发布的所有应用。

仅向用户授予特定应用的访问权限

以下示例展示了如何向用户授予对 Google Play 企业版商店中一组特定应用的访问权限。该过程包括以下步骤:

  • 收集已针对用户 (productSet) 批准的 productIds(应用)列表。
  • 通过将 productSetBehavior 字符串的值设置为 "whitelist",指定用户只能访问给定列表中的应用。
  • 使用 setAvailableProductSetproductIds 列表和行为设置应用于用户的可用商品集。
public ProductSet setProductSet(String enterpriseId, String userId,
    List<String> productIds) throws IOException {
  ProductSet productSet = new ProductSet();
  productSet.setProductId(productIds);
  productSet.setProductSetBehavior("whitelist");

  return androidEnterprise
      .users()
      .setAvailableProductSet(enterpriseId, userId, productSet)
      .execute();
}

授权用户访问所有已批准的应用

以下示例展示了如何从 Google Play 企业版商店授权用户访问任何已获批企业的应用。该过程包括以下步骤:

  • 通过将 productSetBehavior 字符串的值设置为 "allApproved",指定用户可以访问已批准用于企业的所有应用。
  • 使用 setAvailableProductSet 将此设置应用于用户。
public ProductSet setUserIncludeApprovedApps(String enterpriseId, String userId)
    throws IOException {
  ProductSet productSet = new ProductSet();
  productSet.setProductSetBehavior("allApproved");

  return androidEnterprise
      .users()
      .setAvailableProductSet(enterpriseId, userId, productSet)
      .execute();
}

注意:将 productSetBehavior 设置为 "allApproved" 时,您无需为 productSet 指定任何 productIds

向用户授予对所有应用的访问权限

以下示例展示了如何授权用户在 Google Play 企业版商店中访问 Google Play 企业版商店中提供的任何应用。有权访问所有应用的用户在打开 Google Play 企业版时,仍然只能看到其企业的商店布局,但可以通过搜索找到其他应用。

在为企业批准应用之前,某些受信任的用户(例如 IT 管理员)可能要求具有更高的访问权限来测试和评估应用。向用户授予对所有应用的访问权限的过程包含以下步骤:

  • 通过将 productSetBehavior 字符串的值设置为 "includeAll",指定用户可以访问 Google Play 商店中的所有应用。
  • 使用 setAvailableProductSet 将此设置应用于用户。
public ProductSet setUserIncludeAllApps(String enterpriseId, String userId)
    throws IOException {
  ProductSet productSet = new ProductSet();
  productSet.setProductSetBehavior("includeAll");

  return androidEnterprise
      .users()
      .setAvailableProductSet(enterpriseId, userId, productSet)
      .execute();
}

注意:将 productSetBehavior 设置为 "includeAll" 时,您无需为 productSet 指定任何 productIds

创建商店布局

向用户授予应用访问权限后,将应用分组,以便在 Google Play 企业版商店中展示。

您可以使用 Storelayoutpages Storelayoutclusters API 为每个客户创建独特的自定义商店布局。典型的布局由一组页面组成,每个页面都可以包含一组应用。您可以将相关应用放入同一集群中。如需了解详情和查看示例代码,请参阅创建自定义店铺布局

获取应用的权限

如需将应用添加到自定义商店布局(或在用户的设备上静默安装应用)并显示该应用以供用户选择,管理员必须接受整个组织对该应用的权限。管理员可以在 Google Play 企业版管理中心中接受应用权限,并将应用标记为已获准分发(请参阅 Google Play 企业版概览)。

我们建议的最佳实践是将权限 iframe 嵌入到 EMM 控制台中。这意味着,用户不必单独导航到 Google Play 企业版管理中心。使用 Products.getApprovalUrl 获取 iframe 的网址。

public String getApprovalUrl(String enterpriseId, String productId)
    throws IOException {
  return androidEnterprise
      .products()
      .generateApprovalUrl(enterpriseId, productId)
      .execute()
      .getUrl();
}

请使用 Products.approve 接受这些权限。

public void approveProduct(String enterpriseId,
    String productId,
    String approvalUrl) throws IOException {
  ProductsApproveRequest productsApproveRequest =
      new ProductsApproveRequest()
          .setApprovalUrlInfo(
              new ApprovalUrlInfo().setApprovalUrl(approvalUrl));
  androidEnterprise
      .products()
      .approve(enterpriseId, productId, productsApproveRequest)
      .execute();
  }

获取用户的设备

如需执行设备专用操作,您需要识别与用户关联的设备。此示例使用 DevicesListResponse 返回给定 userID 的设备列表。

public DevicesListResponse list(String enterpriseId, String userId) throws
   IOException {

    return androidEnterprise
        .devices()
        .list(enterpriseId, userId)
        .execute();
}

如果用户同意了在首次尝试访问 Google Play 企业版时呈现的服务条款,响应中就会包含非受管设备(管理类型为 unmanagedProfile 的设备)。

获取和设置设备的状态

此管理任务仅适用于使用 Google 帐号的组织。不适用于使用 Google Play 企业版帐号的组织。

在受管设备上激活用户的受管理 Google 帐号后,系统会根据以下因素启用(或停用)对 Google 服务的访问权限:

如果已停用 EMM 强制执行,那么只要在 Android 设备上激活了帐号,系统就会忽略设备状态,并授予帐号对 Google 服务的访问权限。如果已启用 EMM 强制执行,但设备的状态未启用,则用户将无法从 Google Play 安装应用,并且 Google Play EMM API 也无法在设备上静默地为该用户安装应用。

此示例展示了如何获取给定设备的状态(通过传递 enterpriseIduserIddeviceId 来指定)。

getState()setState() 操作仅适用于 managementTypemanagedDevicemanagedProfile(针对 Devices 资源)的设备。这些 API 无法控制 managementTypeunmanagedProfile 的设备。

public DeviceState getState(String enterpriseId, String userId, String
   deviceId) throws IOException {

    return androidEnterprise
        .devices()
        .getState(enterpriseId, userId, deviceId)
        .execute();
}

通过将 AccountState 字符串设置为相应的常量来为设备上的帐号启用或停用 Google 服务。

public DeviceState setState(String enterpriseId, String userId, String
   deviceId, String accountState) throws IOException {

    DeviceState deviceState = new DeviceState();
    deviceState.setAccountState(accountState);

    return androidEnterprise
        .devices()
        .setState(enterpriseId, userId, deviceId, deviceState)
        .execute();
}

在设备上推送安装应用

管理员可以静默安装应用。用户不需要互动。 此示例使用 Installs.Update 以静默方式将应用(由 productId 标识)安装到设备(由 deviceId 标识)。

public Install update(String enterpriseId, String userId, String
   deviceId, String productId) throws IOException {

    return androidEnterprise
        .installs()
        .update(enterpriseId, userId, deviceId, productId,
           new Install())
        .execute();
}

如果设备上已有该应用,且有可用的更新版本,则该应用会更新到新版本。

在已批准的应用列表中搜索并获取应用

管理员可以在 Google Play 企业版中创建和管理供用户使用的已批准应用列表。使用自带工作资料且使用工作资料的用户,以及使用公司自有设备(整个设备由组织管理)的用户,只能从此已批准列表中下载应用。

您可以自定义 EMM 控制台,在其中添加搜索框,以便管理员仅搜索已批准的应用列表中的应用。搜索功能接受与标准 Google Play 企业版搜索功能相同的参数,但只会搜索已批准的应用列表中的应用。

例如,如果管理员想要将已批准的应用列表中的某款应用安装推送到其企业的设备,可以使用 EMM 控制台中的此搜索功能查找该应用。

在查询中,您可以指定结果中应包含的商品数量上限,例如 setMaxResults(10L),这样的内容应能填满整个屏幕。默认值为 100,这也是一次可返回的最大数值。如果结果包含分页令牌,您可以通过传递分页令牌来检索更多结果。

此示例展示了如何获取效率应用搜索的前 10 个结果。 请参阅Products.List

public List searchProducts(String enterpriseId) throws IOException {
  ProductsListResponse executeResult =
      androidEnterprise
          .products()
          .list(enterpriseId)
          .setMaxResults(10L)
          .setQuery("productivity")
          .execute();
  return executeResult.getProduct();
}

如果搜索结果包含分页令牌,则搜索生成的结果数超过 setMaxResults 个,在此示例中为超过 10 个。要检索更多结果,请重新执行搜索并在请求中包含分页令牌。此示例获取接下来的 10 个结果。

public ProductsListResponse continueSearch(
    String enterpriseId, ProductsListResponse previousResponse) throws IOException {
  ProductsListResponse nextResults =
      androidEnterprise
          .products()
          .list(enterpriseId)
          .setMaxResults(10L)
          .setToken(previousResponse.getTokenPagination().getNextPageToken())
          .setQuery("productivity")
          .execute();
  return nextResults;
}