Detect App being removed from Phone/Device Administrator

1.1k views Asked by At

There are some apps which can detect when they are being removed from Phone/Device Administrator. I've searched on Android developer website and couldn't found out the flag or receiver which triggers when user clicks on that "tick" checkbox beside our application in the Phone/Device Administrator.

2

There are 2 answers

3
Ahmad Shahwaiz On BEST ANSWER

In the Broadcast receiver, there is an callback function which extends from DeviceAdminReceiver class which is as follows. Once user clicks the deactivate button this function is called onDisableRequested right before disabling the app from device administrator, after user clicks deactivate it calls onDisabled. First of all we have to call the launcher (home screen) after that lock the device. User won't be able to deactivate if we use this logic. If is there any more optimized way feel free to share/update.

@Override
    public CharSequence onDisableRequested(Context context, Intent intent) { 
            Intent homeScreenIntent = new Intent(Intent.ACTION_MAIN);
            homeScreenIntent.addCategory(Intent.CATEGORY_HOME);
            homeScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(homeScreenIntent);
            DevicePolicyManager deviceManger;
            deviceManger = (DevicePolicyManager) context.getSystemService(
                    Context.DEVICE_POLICY_SERVICE);
            deviceManger.lockNow();
         return context.getString("App won't work if you disable this setting");
    }
4
Fred On

A device administrator receives the action ACTION_DEVICE_ADMIN_DISABLED when it gets disabled by the user, which you can handle in onDisabled(Context, Intent). You can still use the DevicePolicyManager privileged APIs within the onDisabled method, but not after it returns.