管理應用程式角色

IT 管理員可透過應用程式角色功能,在 Android 裝置上授予受管理應用程式特殊權限。指派特定角色後,應用程式可免除電力和背景限制、暫停、休眠 (Android 14 以上版本),並停用使用者控制項 (即包括強制停止和清除應用程式資料等使用者動作) (Android 11 以上版本),以便不間斷地執行重要功能。此外,應用程式可以收到指派的角色通知,因此不需要使用者介入即可自行啟動。

必要條件

裝置是由以 AMAPI 為基礎的 EMM 管理 (不支援使用自訂 DPC 的 EMM)。

準備好應用程式以使用這項功能

只有在應用程式想收到指派角色的通知,以便自行啟動 (即自動啟動,不需使用者互動) 時,才需要整合 AMAPI SDK。

在應用程式中整合 AMAPI SDK

如要進一步瞭解 AMAPI SDK,以及如何將其新增至應用程式,請參閱 AMAPI SDK 整合指南

在應用程式的資訊清單中新增必要的中繼資料

Android Device Policy (ADP) 需要知道實作 NotificationReceiverService 的類別 ComponentName,才能將指派的角色通知應用程式。您必須在 AndroidManifest.xml 中適當標記服務,ADP 才能自動探索服務。

  • 應用程式必須只有一個 enabled 服務,且 meta-dataandroid:name 等於 com.google.android.managementapi.notification.NotificationReceiverService.SERVICE_APP_ROLES
  • 這項服務的 android:exported 必須設為 true
  • meta-dataandroid:value 必須設為空字串
<service
 android:name=".MyNotificationReceiverService"
 android:exported="true">
    <meta-data android:name="com.google.android.managementapi.notification.NotificationReceiverService.SERVICE_APP_ROLES" android:value="" />
</service>

如果您要測試 COMPANION_APP 角色,也應在服務中新增下列 meta-data,讓 Android Device Policy 可以將本機指令狀態更新傳送至應用程式:

<meta-data android:name="com.google.android.managementapi.notification.NotificationReceiverService.SERVICE_COMMAND_STATUS" android:value="" />

建立擴充 NotificationReceiverService 的服務 (或更新現有服務)

建立或更新現有的 NotificationReceiverService,並實作 AppRolesListener,監聽指派給應用程式的角色。監聽指派給應用程式的角色時,只需要 getAppRolesListener()。如果應用程式獲派 COMPANION_APP 角色,您也應實作 getCommandListener()

import android.util.Log
import com.google.android.managementapi.approles.AppRolesListener
import com.google.android.managementapi.approles.model.AppRolesSetRequest
import com.google.android.managementapi.approles.model.AppRolesSetResponse
import com.google.android.managementapi.commands.CommandListener
import com.google.android.managementapi.commands.model.Command
import com.google.android.managementapi.notification.NotificationReceiverService

class MyNotificationReceiverService : NotificationReceiverService() {

  // If your app wants to listen for roles assigned
  override fun getAppRolesListener(): AppRolesListener =
    object : AppRolesListener {
      override fun onAppRolesSet(request: AppRolesSetRequest): AppRolesSetResponse {
        val roleTypes = request.roles.map { role -> role.roleType }
        Log.i(TAG, "onAppRolesSet: $roleTypes")

        return AppRolesSetResponse.getDefaultInstance()
      }
    }

 // If your app wants to listen for local command status updates
 // Only relevant for COMPANION_APP role
 override fun getCommandListener(): CommandListener {
    return object : CommandListener {
      override fun onCommandStatusChanged(command: Command) {
         Log.i(TAG, "onCommandStatusChanged")
      }
    }
  }

  private companion object {
    const val TAG = "MyNotificationReceiverService"
  }
}

如果應用程式的角色多次變更,應用程式可能會收到多則通知。如果移除所有角色,應用程式仍會收到通知,但角色清單會是空白。這項通知會讓應用程式脫離停止狀態,加上應用程式獲得的豁免權,應用程式就能自行啟動,不需使用者互動。由於應用程式角色通知和豁免,您的應用程式可以監聽 ACTION_BOOT_COMPLETED 廣播。如果應用程式需要受管理設定才能啟動,請參閱「設定受管理設定」,瞭解如何讀取及監聽變更。

使用應用程式角色政策佈建裝置

應用程式開發人員可以使用 EMM 測試為應用程式指派角色,或按照 Android Management API 快速入門導覽課程操作。您可以使用 AMAPI Colab 筆記本註冊企業、建立政策及佈建裝置。

使用應用程式角色設定應用程式政策

使用 ApplicationPolicy.roles 設定 policy,並指定應用程式應有的角色。

以下範例說明如何為 MTD 應用程式設定角色:

{
  "applications": [

    {
      "packageName": "com.example.mtd",
      "installType": "FORCE_INSTALLED",
      "roles": [
        { "roleType": "MOBILE_THREAT_DEFENSE_ENDPOINT_DETECTION_RESPONSE" }
      ]
    }
  ]
}

在指派政策中指定的角色前,系統會先檢查裝置上的應用程式簽署金鑰憑證指紋是否與 Play 商店中的相符。如果指紋不同,系統不會將角色指派給應用程式,並會向 EMM 回報 NonComplianceReason.APP_SIGNING_CERT_MISMATCH 不符規定。

{
  "applications": [

    {
      "packageName": "com.example.mtd",
      "installType": "FORCE_INSTALLED",
      "signingKeyCerts": [
         { "signingKeyCertFingerprintSha256": "base64-encoded-sha256" }
       ],
      "roles": [
        { "roleType": "MOBILE_THREAT_DEFENSE_ENDPOINT_DETECTION_RESPONSE" }
      ]
    }
  ]
}

如果應用程式有受管理的設定,IT 管理員可以在應用程式政策中,為相關的 restriction 設定初始設定:

{
  "applications": [

    {
      "packageName": "com.example.mtd",
      "installType": "FORCE_INSTALLED",
      "roles": [
        { "roleType": "MOBILE_THREAT_DEFENSE_ENDPOINT_DETECTION_RESPONSE" }
      ],
      "managedConfiguration": {
        "<key>": "<value>"
      }
    }
  ]
}