【问题标题】:Android for work - How to check if my application is running in the work profile?Android for work - 如何检查我的应用程序是否在工作配置文件中运行?
【发布时间】:2021-02-12 03:42:06
【问题描述】:

我正在创建一个应用,如果它在工作配置文件中运行,它的行为需要有所不同。

有可能知道吗?

文档对此一无所知,我已经尝试添加仅在工作配置文件中可用的限制并且它有效,但我需要一个无需管理员采取任何行动的解决方案。

Android 工作信息: http://www.android.com/work/

Android 工作文档: https://developer.android.com/training/enterprise/index.html

【问题讨论】:

  • 您是否设法确定应用何时在工作资料上运行?
  • 很遗憾没有。如果你发现了什么,请告诉我。

标签: android


【解决方案1】:

我找到了解决方案: 如果 isProfileOwnerApp 为一个包名称返回 true,则表示您的应用(标记)正在工作配置文件上运行。 如果您的应用在正常模式下运行(无徽章)isProfileOwnerApp 为所有管理员返回 false,即使有工作资料也是如此。

DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
List<ComponentName> activeAdmins =   devicePolicyManager.getActiveAdmins();
if (activeAdmins != null){
   for (ComponentName admin : activeAdmins){
      String packageName=  admin.getPackageName();
      Log.d(TAG, "admin:"+packageName);
      Log.d(TAG, "profile:"+ devicePolicyManager.isProfileOwnerApp(packageName));
      Log.d(TAG, "device:"+ devicePolicyManager.isDeviceOwnerApp(packageName));
   }
}

【讨论】:

  • 你怎么知道是work profile?还是所有配置文件都是工作配置文件?
  • @Jason if (devicePolicyManager.isProfileOwnerApp(packageName)) return true; 在循环内,如果一个包返回真,那么你在工作资料中。否则,你不是。
  • 在我的情况下,我在小米和 activeadmins 中都得到了 false 和 null,我正在使用 Island 应用程序作为工作资料。
【解决方案2】:

我将@earlypearl 的回复放入了一个 Kotlin 类:

class UserProfile(context: Context) {
    private val weakContext = WeakReference(context)

    val isInstalledOnWorkProfile: Boolean
        get() {
            return weakContext.get()?.let {
                val devicePolicyManager =
                    it.getSystemService(AppCompatActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
                val activeAdmins = devicePolicyManager.activeAdmins

                activeAdmins?.any { devicePolicyManager.isProfileOwnerApp(it.packageName) } ?: false
            } ?: false
        }
}

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多