AMAPI SDK ช่วยให้แอปส่วนขยายที่ระบุโดย EMM สื่อสารกับ Android Device Policy ได้โดยตรง
ซึ่งขณะนี้รองรับการดำเนินการในเครื่องของ Commands
และเฉพาะคำสั่ง ClearAppData
เท่านั้น คุณต้องทำตามขั้นตอนต่อไปนี้เพื่อผสานรวมกับ SDK
- เพิ่มไลบรารีในแอปส่วนขยาย
- ใช้ API ที่มีให้เพื่อออกคำสั่งตามที่จำเป็น
- เพิ่มองค์ประกอบการค้นหา หาก SDK เป้าหมาย >= 30
- คุณดำเนินการให้บริการเพื่อฟังโค้ดเรียกกลับการเปลี่ยนแปลงสถานะคำสั่งได้ (ไม่บังคับ)
- จัดสรรอุปกรณ์โดยใช้นโยบายการขยายการใช้งาน
ข้อกำหนดเบื้องต้น
- ตรวจสอบว่าได้ตั้งค่า minSdkVersion ของแอปส่วนขยายเป็น API ระดับ 21 เป็นอย่างน้อย
กำลังเพิ่มไลบรารีในแอปพลิเคชันส่วนขยาย
ในไฟล์ Build.gradle ระดับบนสุด ให้เพิ่มที่เก็บ Google Maven ซึ่งมีไลบรารี SDK ลงในโมดูลที่เกี่ยวข้องและเพิ่มทรัพยากร Dependency ไปยังไลบรารี
repositories {
...
google()
}
จากนั้นเพิ่มไลบรารีในบล็อกทรัพยากร Dependency ของโมดูลด้วยคำสั่งต่อไปนี้
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();
}
...
ตัวอย่างด้านบนแสดงการออกคำขอข้อมูลแอปที่ชัดเจนสำหรับแพ็กเกจที่ระบุและรอจนกว่าจะออกคำสั่งได้สำเร็จ หากออบเจ็กต์ Command สร้างสำเร็จจะแสดงสถานะคำสั่งปัจจุบันและรหัสคำสั่งที่นำไปใช้ค้นหาสถานะของคำสั่งที่ใช้เวลานานได้ในภายหลัง
รับคำสั่ง
แอปส่วนขยายยังค้นหาสถานะคำขอคำสั่งที่ออกก่อนหน้านี้ได้ด้วย หากต้องการเรียกข้อมูลสถานะของคำสั่ง คุณจะต้องใช้รหัสคำสั่ง (มีอยู่ในคำขอคำสั่งปัญหา) ข้อมูลโค้ดต่อไปนี้แสดงการส่งคำขอ GetCommand ไปยัง 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());
}
...
การเพิ่มองค์ประกอบข้อความค้นหา
หากแอปกำหนดเป้าหมายเป็น SDK 30 ขึ้นไป คุณต้องระบุองค์ประกอบการค้นหาในไฟล์ Manifest เพื่อระบุว่าแอปจะโต้ตอบกับ ADP
<queries>
<package android:name="com.google.android.apps.work.clouddpc" />
</queries>
ดูข้อมูลเพิ่มเติมได้ที่การกรองระดับการเข้าถึงแพ็กเกจใน Android
ฟังโค้ดเรียกกลับการเปลี่ยนสถานะคำสั่ง
- การเปลี่ยนแปลงสถานะคำสั่งจะได้รับแจ้งไปยัง CommandListener, ใช้อินเทอร์เฟซนี้ในแอป และบอกวิธีจัดการกับการอัปเดตสถานะที่ได้รับ
- ขยาย NotificationReceiverService และมอบอินสแตนซ์ CommandListener
ระบุชื่อคลาสของ NotificationReceiverService แบบขยายในนโยบาย Android Management API (ดูการกำหนดค่านโยบาย)
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"
}
}]
การทดสอบ
การทดสอบ 1 หน่วย
LocalCommandClient เป็นอินเทอร์เฟซ ดังนั้นการทดสอบจึงทำให้สามารถติดตั้งใช้งานที่ทดสอบได้
การทดสอบการผสานรวม
ต้องใช้ข้อมูลต่อไปนี้เพื่อทดสอบกับ Android Device Policy
- ชื่อแพ็กเกจของแอปส่วนขยาย
- แฮช SHA-256 ที่เข้ารหัสเลขฐาน 16 ของลายเซ็นที่เชื่อมโยงกับแพ็กเกจแอป
- (ไม่บังคับ) หากทดสอบโค้ดเรียกกลับ - ชื่อที่ตรงตามเกณฑ์ทั้งหมดของบริการจากบริการที่เพิ่งเปิดตัวเพื่อรองรับโค้ดเรียกกลับ (ชื่อแบบเต็มของ CommandService ในตัวอย่าง)