Runtime.getRuntime().exec("dpm set-device-owner com.integral.kiosk.kioskapp/.receivers.MyDeviceAdminReceiver");
没有在 Android 模拟器 Nexus 6 OS Android 8 中设置设备所有者。但是当使用 adb shell 命令时它工作正常。
附上我在 oncreate 函数中使用的代码。
ComponentName deviceAdmin = new ComponentName(this, MyDeviceAdminReceiver.class);
if (!mDevicePolicyManager.isAdminActive(deviceAdmin)) {
Toast.makeText(this, "Not Device Admin", Toast.LENGTH_SHORT).show();
Intent activateDeviceAdmin = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdmin.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin
);
activateDeviceAdmin
.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "After activating admin, you will be able to block application uninstallation.”");
startActivityForResult(activateDeviceAdmin,
PM_ACTIVATION_REQUEST_CODE);
}
if (mDevicePolicyManager.isDeviceOwnerApp(getPackageName())) {
mDevicePolicyManager.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
Toast.makeText(this, "Device Owner", Toast.LENGTH_SHORT).show();
// startLockTask();
mDevicePolicyManager.clearDeviceOwnerApp(getPackageName());
} else {
Toast.makeText(this, "Not Device Owner", Toast.LENGTH_SHORT).show();
try {
Runtime.getRuntime().exec("dpm set-device-owner com.integral.kiosk.kioskapp/.receivers.MyDeviceAdminReceiver");
} catch (IOException e) {
e.printStackTrace();
}
请帮忙。提前致谢