检测到正在从 Phone/Device 管理员中删除应用程序

Detect App being removed from Phone/Device Administrator

有些应用程序可以检测到它们何时从 Phone/Device 管理员中删除。我在 Android 开发者网站上搜索过,但找不到当用户单击 Phone/Device 管理员中我们应用程序旁边的 "tick" 复选框时触发的标志或接收器。

设备管理员收到操作 ACTION_DEVICE_ADMIN_DISABLED when it gets disabled by the user, which you can handle in onDisabled(Context, Intent)。您仍然可以在 onDisabled 方法中使用 DevicePolicyManager 特权 API,但不能在它之后 returns.

在Broadcast receiver中,有一个从DeviceAdminReceiver class扩展而来的回调函数,如下所示。一旦用户单击停用按钮,就在设备管理员禁用应用程序之前调用此函数 onDisableRequested,在用户单击停用后它调用 onDisabled。 首先,我们必须在锁定设备后调用启动器(主屏幕)。 如果我们使用此逻辑,用户将无法停用。如果有任何更优化的方式随时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");
    }