AMAPI SDK 使 EMM 指定的扩展应用能够直接与 Android Device Policy 通信。目前,它支持本地执行 Commands
,且仅支持 ClearAppData
命令。若要与 SDK 集成,必须执行以下步骤:
- 向扩展程序应用添加库。
- 根据需要利用提供的 API 发出命令。
- 如果目标 SDK 不低于 30,请添加查询元素。
- 或者,您可以提供服务实现来监听命令状态更改回调。
- 为设备配置可扩展性政策。
前提条件
- 确保扩展程序应用的 minSdkVersion 至少设置为 API 级别 21。
向扩展程序应用添加库
在顶级 build.gradle 文件中,将包含 SDK 库的 Google Maven 制品库添加到相关模块,并将依赖项添加到库:
repositories {
...
google()
}
然后将该库添加到模块的依赖项代码块中:
dependencies {
implementation 'com.google.android.libraries.enterprise.amapi:amapi:1.0.0'
}
向 Android Device Policy 发送请求
现在应该可以向 ADP 发送请求了。支持以下请求。
发出命令
扩展程序应用可以请求使用 ADP 发出命令。IssueCommandRequest 包含的请求对象将包含要发出的命令和特定参数的详细信息。如需了解详情,请参阅 Javadoc。
以下代码段展示了如何发出清除软件包数据的请求:
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();
}
...
上面的示例展示了如何为指定软件包发出清除应用数据请求,并等待命令成功发出。如果成功发出一个命令对象,则该对象将返回当前命令状态和命令 ID,稍后可使用该命令查询任何长时间运行的命令的状态。
获取命令
扩展程序应用还可以查询之前发出的命令请求的状态。如需检索命令的状态,您需要命令 ID(可从发出命令请求中找到)。以下代码段展示了如何向 ADP 发送 GetCommand 请求。
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());
}
...
添加查询元素
如果您的应用以 SDK 30 及更高版本为目标平台,则需要在清单中定义查询元素,以指定它将与 ADP 交互。
<queries>
<package android:name="com.google.android.apps.work.clouddpc" />
</queries>
如需了解详情,请参阅 Android 上的软件包可见性过滤 。
监听命令状态更改回调
- 向 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" />
策略配置
"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 是一个接口,因此测试可让您轻松提供可测试的实现。
集成测试
要使用 Android Device Policy 进行测试,您需要提供以下信息:
- 扩展程序应用的软件包名称。
- 与应用软件包关联的签名的十六进制编码 SHA-256 哈希。
- (可选)如果要测试回调 - 新引入的服务中用于支持回调的服务的完全限定名称。(示例中 CommandService 的完全限定名称)。