检测从电话/设备管理员中删除的应用程序

Ahm*_*aiz 5 android broadcastreceiver device-admin

有一些应用程序可以检测何时从电话/设备管理员中删除它们。我在 Android 开发人员网站上搜索过,但找不到当用户单击电话/设备管理器中应用程序旁边的“勾号”复选框时触发的标志或接收器。

Ahm*_*aiz 1

在广播接收器中,有一个从DeviceAdminReceiver类扩展的回调函数,如下所示。一旦用户单击停用按钮,此函数就会onDisableRequested在设备管理员禁用应用程序之前调用,在用户单击停用后,它会调用onDisabled。首先,我们必须在锁定设备后调用启动器(主屏幕)。如果我们使用此逻辑,用户将无法停用。如果有任何更优化的方法请随时分享/更新。

@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");
    }
Run Code Online (Sandbox Code Playgroud)