android - DeviceOwner: Trying to set device owner but device is already provisioned

客户写了一个apk要成为device owner;
之后apk联网更新升级;
强制卸载安装,导致无法再次成为DeviceOwner。

原因:当一个app成为DeviceOwner后,这个app是不能被卸载,但是由于应用更新,强制卸载之前没有注销DeviceOwner,导致异常。

解决方案:
https://blog.csdn.net/u011068702/article/details/53191952

针对问题进行解决的过程:
1.adb shell dpm set-device-owner io.shoonya.shoonyadpc/com.shoonyaos.shoonyadpc.receivers.AdminReceiver
java.lang.IllegalStateException: Trying to set device owner but device is already provisioned.
at android.os.Parcel.readException(Parcel.java:1554)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.admin.IDevicePolicyManager S t u b Stub StubProxy.setDeviceOwner(IDevicePolicyManager.java:3212)
at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:114)
at com.android.commands.dpm.Dpm.onRun(Dpm.java:82)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.dpm.Dpm.main(Dpm.java:38)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)

2.adb shell dumpsys account
User UserInfo{0:机主:13}:
Accounts: 1
Account {name=PHONE, type=com.android.localphone}

Active Sessions: 0

RegisteredServicesCache: 4 services
ServiceInfo: AuthenticatorDescription {type=com.qualcomm.RIDL}, ComponentInfo{com.qualcomm.RIDL/com.qualcomm.RIDL.cService}, uid 1000
ServiceInfo: AuthenticatorDescription {type=com.android.localphone}, ComponentInfo{com.qualcomm.simcontacts/com.qualcomm.simcontacts.PhoneAuthenticateService}, uid 1000
ServiceInfo: AuthenticatorDescription {type=com.android.sim}, ComponentInfo{com.qualcomm.simcontacts/com.qualcomm.simcontacts.SimAuthenticateService}, uid 1000
ServiceInfo: AuthenticatorDescription {type=com.qualcomm.qti.calendarlocalaccount}, ComponentInfo{com.qualcomm.qti.calendarlocalaccount/com.qualcomm.qti.calendarlocalaccount.AuthenticationService}, uid 10024

3.adb shell pm hide com.qualcomm.simcontacts
Package com.qualcomm.simcontacts new hidden state: true

4.adb shell dpm set-device-owner io.shoonya.shoonyadpc/com.shoonyaos.shoonyadpc.receivers.AdminReceiver
Success: Device owner set to package io.shoonya.shoonyadpc
Active admin set to component {io.shoonya.shoonyadpc/com.shoonyaos.shoonyadpc.receivers.AdminReceiver}

5.adb shell pm unhide com.qualcomm.simcontacts
Package com.qualcomm.simcontacts new hidden state: false

这个临时方案是在设备有adb的情况下,但是根据复现问题的现象来看,每次应用更新就要操作一边很麻烦,并且以后发布user版本没有adb就无法解决这个问题。

最好的方法是应用在更新之前调用:
devicePolicyManager.clearDeviceOwnerApp(getPackageName());
解除Device Owner身份

另外,
adb shell dpm remove-active-admin 指令需要android:testOnly才能移除。

移除DeviceOwner
当一个app成为DeviceOwner后,这个app是不能被卸载,也无法在设置->安全中关闭其权限。要想DeviceOwner后还能卸载这个app,也就是退出DeviceOwner,有如下方法:

devicePolicyManager.clearDeviceOwnerApp(packageName)
1,在AndroidManifest.xml中的节点添加android:testOnly="true";
2,通过命令adb install -t examole.apk安装该app;
3,通过命令adb shell dpm set-device-owner com.example.sample/.MyDeviceAdminReceiver成为DeviceOwner;
4,通过命令adb shell dpm remove-active-admin com.example.sample/.MyDeviceAdminReceive退出DeviceOwner;

偶然百度到了一个DEMO:
https://github.com/ddssingsong/DevicePolicyManager

你可能感兴趣的:(android_system,android)