客戶寫了一個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.IDevicePolicyManagerProxy.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中的<application/>節點添加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