The broadcast receiver has a callback function that extends from the class DeviceAdminReceiver, which is as follows. When the user presses the deactivation button, this function is called onDisableRequestedright before the application is disconnected from the device administrator, after the user presses the deactivate button, it calls onDisabled. First of all, we need to call the launchpad (home screen), and then lock the device. The user will not be able to deactivate if we use this logic. If there is a 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");
}
source
share