EMM 指定的擴充應用程式可透過 Android Management API (AMAPI) SDK,直接與 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,並透過getCommandListener方法提供CommandListener例項。 設定應用程式政策,將
COMPANION_APP角色指派給應用程式 (請參閱「政策設定」)。import com.google.android.managementapi.commands.CommandListener; import com.google.android.managementapi.notification.NotificationReceiverService; ... public class SampleCommandService extends NotificationReceiverService { @Override public CommandListener getCommandListener() { // return the concrete implementation from previous step return ...; } }
政策設定
如要讓擴充功能應用程式直接與 ADP 通訊,EMM 必須使用應用程式政策中的 roles 欄位,將 COMPANION_APP 角色指派給應用程式。
"applications": [{
"packageName": "com.amapi.extensibility.demo",
"installType": "FORCE_INSTALLED",
"roles": [
{ "roleType": "COMPANION_APP" }
]
}]
如要查看其他可用選項,請參閱「透過應用程式角色政策佈建裝置」。
測試
單元測試
LocalCommandClient 是介面,因此可提供可測試的實作項目。
整合測試
如要使用 ADP 進行測試,請提供下列資訊:
- 擴充應用程式的套件名稱。
- 與應用程式套件相關聯的簽章 SHA-256 雜湊 (以 Base64 編碼)。
- (選用) 如果要測試回呼,請提供新導入服務的完整名稱,以支援回呼。(範例中
CommandService的完整名稱)。