Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect App being removed from Phone/Device Administrator

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.

like image 718
Ahmad Shahwaiz Avatar asked Nov 07 '22 17:11

Ahmad Shahwaiz


1 Answers

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");
    }
like image 55
Ahmad Shahwaiz Avatar answered Dec 15 '22 18:12

Ahmad Shahwaiz