借助 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 稍后可用于查询任何长时间运行的命令的状态。
获取命令
扩展程序应用可以查询之前发出的命令请求的状态。如需检索命令的状态,您需要命令 ID(可从发出命令请求中获取)。以下代码段展示了如何向 ADP 发送
GetCommandRequest。
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 进行测试,您需要以下信息:
- 扩展程序应用的软件包名称。
- 与应用软件包关联的签名的 Base64 编码 SHA-256 哈希值。
- (可选)如果要测试回调,则需要提供新引入的服务的完全限定名称,以支持回调。(示例中
CommandService的完全限定名称)。