5

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.

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35

2 Answers2

1

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");
    }
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35
  • Your question was about *detecting* when the user removes the device administrator, not about preventing it. This doesn't answer the question. – Fred Aug 30 '17 at 18:19
  • In addition, since users cannot uninstall a device administrator this will prevent users from ever uninstalling your app and they will be forced to factory reset their devices. Unless you want bad reviews for your app you shouldn't do that. – Fred Aug 30 '17 at 18:25
  • my question was about the "detect app 'being' removed" and your answer didn't qualify because you gave onDisabled(Context, Intent) function as answer, which is not true. You have to use onDisableRequested to know if they are 'being' removed. The one you told tells you that it has been removed (not going to being removed.). In addition to your second comment, A flag needs to be set on onDisableRequested function to prevent users to prevent uninstall all the time, just a preference check nothing more. – Ahmad Shahwaiz Aug 31 '17 at 08:09
0

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.

Fred
  • 2,191
  • 1
  • 12
  • 14
  • But I want to prevent it from disabling my app. If I write something in onDisabled then user already has disabled it. There is another function onDisableRequested(Context context, Intent intent) in this function I can write a warning message or so before user disables. However If I lock the phone here pro-grammatically, it still continue from same screen after user unlocks the phone. How can I prevent it? – Ahmad Shahwaiz Aug 30 '17 at 09:12
  • The best you can do is wipe the device (factory reset) if the user tries to disable your device administrator (in onDisabled), this would delete any sensible data you wish to protect. Since the user can wipe the device anyway and it would remove the device administrator, this is the best you can do. – Fred Aug 30 '17 at 09:35
  • If you're looking for more powerful capabilities like preventing factory reset or preventing use of the device after factory reset, you can try the Device Owner mode which requires being set when the device is first setup. More details on the two modes in [this response of mine](https://stackoverflow.com/a/45632137/4816506). – Fred Aug 30 '17 at 09:42
  • There is another way instead of factory reset. Please see my answer. – Ahmad Shahwaiz Aug 30 '17 at 18:11