检测设备 运行 MDM(移动设备管理)

Detect devices running MDM (mobile device management)

我有一个功能可以在旧的应用程序版本中弹出一个应用程序更新提示对话框(该版本是通过 Firebase 远程配置控制的)。

然而,事实证明我的很多(但不是大多数)客户使用 MDM 来锁定他们的 phone,在这种情况下,最终用户无法直接更新应用程序。

我正在使用 intent.resolveActivity(packageManager) 的正常检测来检查播放商店 activity 是否可以在显示对话框之前启动。但该检查通过了 - Play 商店在那里,但更新被阻止。

我想为这些最终用户禁用更新提示。

有没有办法检测 MDM?或者至少有一种检测应用更新已被阻止的方法?

您可以使用用户管理器:

UserManager user_manager = (UserManager) getSystemService(Context.USER_SERVICE);
    if (user_manager.hasUserRestriction("WORK_PROFILE_RESTRICTION")) {
        System.out.print("app under MDM");
     } else {
         System.out.print("app not under MDM");
     }

尽管如果管理员没有设置该值,您将收到 false。如果你是真的,那意味着你肯定是在受管理的个人资料下,但如果你是假的,那可能是漏报。

Test DPC google sample contains a ProvisioningStateUtil 和一些实用方法 :

/**
 * @return true if the device or profile is already owned
 */
public static boolean isManaged(Context context) {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
            Context.DEVICE_POLICY_SERVICE);

    List<ComponentName> admins = devicePolicyManager.getActiveAdmins();
    if (admins == null) return false;
    for (ComponentName admin : admins) {
        String adminPackageName = admin.getPackageName();
        if (devicePolicyManager.isDeviceOwnerApp(adminPackageName)
                || devicePolicyManager.isProfileOwnerApp(adminPackageName)) {
            return true;
        }
    }

    return false;
}

这至少需要 Android 5.0 (API 21)

您也可以直接查看当前用户是否允许安装或更新应用程序:

UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
boolean blocked = userManager.hasUserRestriction(UserManager.DISALLOW_INSTALL_APPS);

还需要API21