Android Management API (AMAPI) SDK 可讓 EMM 指定的擴充功能應用程式直接與 Android Device Policy (ADP) 通訊,並在裝置上執行 Commands
。
與 AMAPI SDK 整合可提供此程式庫的詳細資訊,以及如何將其新增至應用程式。
整合 SDK 後,擴充功能應用程式就能與 ADP 通訊,執行以下操作:
發出指令
擴充功能應用程式可以要求使用 ADP 發出指令。IssueCommandRequest
包含要求物件,內含要發出的指令和特定參數的詳細資料。
下列程式碼片段說明如何發出要求清除包裹資料的要求:
import android.util.Log;
...
import com.google.android.managementapi.commands.LocalCommandClientFactory;
import com.google.android.managementapi.commands.model.Command;
import com.google.android.managementapi.commands.model.GetCommandRequest;
import com.google.android.managementapi.commands.model.IssueCommandRequest;
import com.google.android.managementapi.commands.model.IssueCommandRequest.ClearAppsData;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
...
void issueClearAppDataCommand(ImmutableList<String> packageNames) {
Futures.addCallback(
LocalCommandClientFactory.create(getContext())
.issueCommand(createClearAppRequest(packageNames)),
new FutureCallback<Command>() {
@Override
public void onSuccess(Command result) {
// Process the returned command result here
Log.i(TAG, "Successfully issued command");
}
@Override
public void onFailure(Throwable t) {
Log.e(TAG, "Failed to issue command", t);
}
},
MoreExecutors.directExecutor());
}
IssueCommandRequest createClearAppRequest(ImmutableList<String> packageNames) {
return IssueCommandRequest.builder()
.setClearAppsData(
ClearAppsData.builder()
.setPackageNames(packageNames)
.build()
)
.build();
}
...
上方的範例說明如何針對特定套件發出明確的應用程式資料要求,並等待指令成功發出。如果成功發出,會傳回 Command
物件,其中包含目前的指令狀態和指令 ID,稍後可用來查詢任何長時間執行指令的狀態。
取得指令
擴充功能應用程式可以查詢先前發出的指令要求狀態。如要擷取指令的狀態,您必須取得指令 ID (可透過問題指令要求取得)。下列程式碼片段說明如何將 GetCommandRequest
傳送至 ADP。
import android.util.Log;
...
import com.google.android.managementapi.commands.LocalCommandClientFactory;
...
import com.google.android.managementapi.commands.model.GetCommandRequest;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
...
void getCommand(String commandId) {
Futures.addCallback(
LocalCommandClientFactory.create(getApplication())
.getCommand(GetCommandRequest.builder().setCommandId(commandId).build()),
new FutureCallback<Command>() {
@Override
public void onSuccess(Command result) {
// Process the returned command result here
Log.i(Constants.TAG, "Successfully issued command");
}
@Override
public void onFailure(Throwable t) {
Log.e(Constants.TAG, "Failed to issue command", t);
}
},
MoreExecutors.directExecutor());
}
...
監聽指令狀態變更回呼
擴充功能應用程式可以註冊回呼,接收長時間執行指令的狀態變更更新,步驟如下:
- 系統會向
CommandListener
通知指令狀態變更,並在應用程式中實作這個介面,並提供處理接收狀態更新的方式。 - 擴充
NotificationReceiverService
並提供CommandListener
執行個體。 在 Android Management API 政策中指定擴充的
NotificationReceiverService
類別名稱 (請參閱政策設定)。import com.google.android.managementapi.commands.CommandListener; import com.google.android.managementapi.notification.NotificationReceiverService; ... public class SampleCommandService extends NotificationReceiverService { @Override protected void setupInjection() { // (Optional) If using DI and needs initialisation then use this method. } @Override public CommandListener getCommandListener() { // return the concrete implementation from previous step return ...; } }
將服務新增至
AndroidManifest.xml
,並確認服務已匯出。<service android:name = ".notification.SampleCommandService" android:exported = "true" />
政策設定
如要讓擴充功能應用程式直接與 ADP 通訊,EMM 必須提供 extensionConfig
政策。
"applications": [{
"packageName": "com.amapi.extensibility.demo",
...
"extensionConfig": {
"signingKeyFingerprintsSha256": [
// Include signing key of extension app
],
// Optional if callback is implemented
"notificationReceiver": "com.amapi.extensibility.demo.notification.SampleCommandService"
}
}]
測試
單元測試
LocalCommandClient
是一個介面,因此可提供可測試的實作。
整合測試
如要使用 ADP 進行測試,須提供下列資訊:
- 擴充功能應用程式的套件名稱。
- 與應用程式套件相關聯的簽名中十六進位編碼 SHA-256 雜湊。
- 或者,如果測試回呼,也可以選擇新引導入的服務中支援回呼的完整名稱。(範例中
CommandService
的完整名稱)。