SDK Android Management API (AMAPI) cho phép một ứng dụng tiện ích do EMM chỉ định giao tiếp trực tiếp với Android Device Policy (ADP) và thực thi Commands
trên thiết bị.
Tích hợp với SDK AMAPI cung cấp thêm thông tin về thư viện này và cách thêm thư viện vào ứng dụng của bạn.
Sau khi tích hợp SDK, ứng dụng tiện ích của bạn có thể giao tiếp với ADP để:
Lệnh thực thi
Ứng dụng tiện ích có thể yêu cầu phát hành các lệnh bằng ADP.
IssueCommandRequest
chứa đối tượng yêu cầu sẽ chứa thông tin chi tiết về lệnh sẽ được phát hành và các tham số cụ thể.
Đoạn mã sau đây cho biết cách đưa ra một yêu cầu xoá dữ liệu của gói:
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();
}
...
Ví dụ trước cho thấy việc đưa ra một yêu cầu dữ liệu ứng dụng rõ ràng cho các gói đã chỉ định và chờ cho đến khi lệnh được phát hành thành công. Nếu phát hành thành công, một đối tượng Command
sẽ được trả về cùng với trạng thái lệnh hiện tại và mã lệnh mà sau này có thể được dùng để truy vấn trạng thái của bất kỳ lệnh chạy trong thời gian dài nào.
Nhận lệnh
Ứng dụng tiện ích có thể truy vấn trạng thái của các yêu cầu lệnh đã đưa ra trước đó. Để truy xuất trạng thái của một lệnh, bạn cần có mã lệnh (có trong yêu cầu lệnh phát hành). Đoạn mã sau đây cho biết cách gửi GetCommandRequest
đến 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());
}
...
Nghe lệnh gọi lại thay đổi trạng thái Command
Ứng dụng tiện ích có thể đăng ký lệnh gọi lại để nhận thông tin cập nhật về các thay đổi về trạng thái của các lệnh chạy trong thời gian dài theo các bước sau:
- Các thay đổi về trạng thái lệnh sẽ được thông báo cho
CommandListener
, triển khai giao diện này trong ứng dụng của bạn và cung cấp phương thức triển khai về cách xử lý thông tin cập nhật trạng thái đã nhận. - Mở rộng
NotificationReceiverService
và cung cấp thực thểCommandListener
. Chỉ định tên lớp của
NotificationReceiverService
mở rộng trong chính sách API Quản lý Android (xem Cấu hình chính sách).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 ...; } }
Thêm dịch vụ vào
AndroidManifest.xml
của bạn và đảm bảo rằng dịch vụ đó đã được xuất.<service android:name = ".notification.SampleCommandService" android:exported = "true" />
Cấu hình chính sách
Để cho phép ứng dụng tiện ích giao tiếp trực tiếp với ADP, EMM phải cung cấp chính sách 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"
}
}]
Kiểm thử
Kiểm thử đơn vị
LocalCommandClient
là một giao diện và do đó cho phép cung cấp phương thức triển khai có thể kiểm thử.
Kiểm thử tích hợp
Bạn cần có những thông tin sau để kiểm thử bằng ADP:
- Tên gói của ứng dụng tiện ích.
- Hàm băm SHA-256 được mã hoá theo hệ thập lục phân của Chữ ký liên kết với gói ứng dụng.
- Nếu kiểm thử lệnh gọi lại – thì đây là tên dịch vụ đủ điều kiện từ dịch vụ mới ra mắt để hỗ trợ lệnh gọi lại (không bắt buộc). (Trong ví dụ này là tên đủ điều kiện của
CommandService
).