PC端出现如下log:
D:\workspace\AndroidO\CTS\CTS_Verifier>adb shell dpm set-device-owner 'com.android.cts.verifier/com.android.cts.verifier.managedprovisioning.DeviceAdminTestReceiver'
java.lang.IllegalStateException: Not allowed to set the device owner because there are already some accounts on the device
at android.os.Parcel.readException(Parcel.java:2012)
at android.os.Parcel.readException(Parcel.java:1950)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setDeviceOwner(IDevicePolicyManager.java:5176)
at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:148)
at com.android.commands.dpm.Dpm.onRun(Dpm.java:96)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:54)
at com.android.commands.dpm.Dpm.main(Dpm.java:41)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:285)
目标设备端出现如下log:
05-04 07:43:00.705 788 1904 W DevicePolicyManager: Non test-only owner can't be installed with existing accounts.
05-04 07:43:00.746 788 788 I DevicePolicyManager: Device admin ComponentInfo{com.android.cts.verifier/com.android.cts.verifier.managedprovisioning.DeviceAdminTestReceiver} removed from user 0
分析DevicePolicyManagerService.java的代码找到里面的hasIncompatibleAccountsOrNonAdbNoLock(
int userId, @Nullable ComponentName owner)方法,该方法中有一个条件判断if (owner == null || !isAdminTestOnlyLocked(owner, userId)) 。
在isAdminTestOnlyLocked会判断对应的application的ApplicationInfo.FLAG_TEST_ONLY是否为true,这个变量是通过AndroidManifest.xml中的android:testOnly="true"控制的。
添加之后重新编译,再次执行 success。