[英]Android for work - How to check if my application is running in the work profile?
I'm creating an app that needs to behave differently if it's running in the work profile.我正在创建一个应用程序,如果它在工作配置文件中运行,则该应用程序的行为需要有所不同。
There is any possibility to know that?有没有可能知道?
The documentation has nothing about it and I already tried to add a restriction that is only available in the work profile and it works, but I need a solution without any action from the administrator.该文档对此一无所知,我已经尝试添加仅在工作配置文件中可用的限制并且它有效,但我需要一个无需管理员采取任何操作的解决方案。
Android for work information: http://www.android.com/work/ Android 工作信息: http : //www.android.com/work/
Android for work documentation: https://developer.android.com/training/enterprise/index.html Android 工作文档: https : //developer.android.com/training/enterprise/index.html
I found a solution : if isProfileOwnerApp return true for one package name, it means that your app (badged) is running on work profile.我找到了一个解决方案:如果 isProfileOwnerApp 对一个包名称返回 true,则意味着您的应用程序(标记)正在工作配置文件上运行。 if your app is running in normal mode (no badge) isProfileOwnerApp return false for all admins even if there is a Work profile.
如果您的应用程序在正常模式下运行(无徽章) 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));
}
}
I took the response from @earlypearl into a Kotlin class:我把@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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.