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
,並透過getCommandListener
方法提供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 public CommandListener getCommandListener() { // return the concrete implementation from previous step return ...; } }
政策設定
如要讓擴充功能應用程式直接與 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
的完整合格名稱)。