android – App already set as profile owner, but can't remove it anymore-ThrowExceptions

Exception or error:

I have a class AdminReceiver extending DeviceAdminReceiver in my app.
Previously I made it profile owner via ADB shell

adb shell dpm set-active-admin com.example.myApp/com.example.myApp.AdminReceiver 
adb shell dpm set-profile-owner com.example.myApp/com.example.myApp.AdminReceiver

And everything went fine.
Subsequently, I wanted to remove it executing the following instruction from within my app programmatically:

DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
    try {
    } catch (Exception e) {
        Log.e(TAG, "removingProfile: ", e);

And I get the following exception:

java.lang.SecurityException: Admin ComponentInfo{com.example.myApp/com.example.myApp.AdminReceiver} does not own the profile


When I try to set again the profile owner via ADB I get the following exception

java.lang.IllegalStateException: Trying to set the profile owner, but profile owner is already set


It’s crazy because when i try to remove the profile owner it’s like it doesn’t have it and if i try to make it profile owner it’s like it already has!

Is there any solution without any factory reset of the device?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *