Android for work - 如何检查我的应用程序是否在工作资料中运行?

时间:2015-05-25 16:06:02

标签: android

我正在创建一个应用程序,如果它在工作资料中运行,则需要采取不同的行为。

有可能知道吗?

文档中没有任何内容,我已经尝试添加仅在工作资料中可用的限制,但它可以正常工作,但我需要一个没有管理员任何操作的解决方案。

Android for work information: http://www.android.com/work/

Android for work documentation: https://developer.android.com/training/enterprise/index.html

2 个答案:

答案 0 :(得分:11)

我找到了一个解决方案: 如果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));
   }
}

答案 1 :(得分:0)

我将@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
        }
}
相关问题