11

I'm trying to add a button to my app to remove it from Device Administrator and am using the code below but my app just crashes.

Code:-

On Button Click:-

{
    ComponentName devAdminReceiver = new ComponentName(this, DemoDeviceAdminReceiver.class);
    DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    dpm.removeActiveAdmin(devAdminReceiver);
}

In the code above, DemoDeviceAdminReceiver is a class that extends DeviceAdminReceiver.

Error log:-

10-28 15:26:09.295: E/AndroidRuntime(26101): FATAL EXCEPTION: main
10-28 15:26:09.295: E/AndroidRuntime(26101): java.lang.IllegalArgumentException: Unknown admin: ComponentInfo{com.dragonnis.intellicover/com.dragonnis.intellicover.DemoDeviceAdminReceiver}
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.os.Parcel.readException(Parcel.java:1331)
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.os.Parcel.readException(Parcel.java:1281)
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.app.admin.IDevicePolicyManager$Stub$Proxy.removeActiveAdmin(IDevicePolicyManager.java:2940)
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.app.admin.DevicePolicyManager.removeActiveAdmin(DevicePolicyManager.java:183)

2 Answers2

20

It's as you do:

DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.removeActiveAdmin(mDeviceAdminReceiver);

But you need to add these filters to the receiver in AndroidManifest.xml:

    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
    </intent-filter>


@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
    return "Admin rights are beeing requested to be disabled for the app called: '" + context.getString(R.string.app_name) + "'.";
}
ViliusK
  • 11,345
  • 4
  • 67
  • 71
  • Thanks for the fast response. The intent filters are already present in the AndroidManifest.xml file. Device administrator is enabled just fine but trying to remove is still causing the crash. – Jasjit Singh Marwah Oct 28 '12 at 12:20
  • Oh, fixed it. The issue was with the DeviceAdminReceiver class. I was using a separate class to enable device admin but when I tried to remove it I was using a different DeviceAdminReceiver which was not even enabled as device admin. Silly me. Thanks for your help. – Jasjit Singh Marwah Oct 28 '12 at 12:25
  • @ViliusK can we disable device administrator permission of other app if my app has device admin permission ? – rupesh Oct 06 '15 at 06:43
  • @rup35h No. It's answered here: http://stackoverflow.com/questions/19908226/programmatically-deactivate-a-device-admininstrator-in-android – ViliusK Oct 06 '15 at 16:12
0

wrote an app with device admin enabled (DevicePolicyManager) and installed. But when I want to uninstall it, it returns failed with this message

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 03 '21 at 11:24