In diesem Leitfaden wird veranschaulicht, wie Sie ein Arbeitsprofil auf einem Gerät erkennen. Sie gilt nur für Arbeitsprofile, die von der Android Device Policy App verwaltet werden.
Erkennen, ob die App in einem Arbeitsprofil ausgeführt wird
Mit der folgenden Methode wird geprüft, ob die aufrufende App in einem Arbeitsprofil ausgeführt wird, das von der Android Device Policy App verwaltet wird.
Kotlin
fun isInsideWorkProfile(): Boolean {
val devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
return devicePolicyManager.isProfileOwnerApp("com.google.android.apps.work.clouddpc")
}
Java
boolean isInsideWorkProfile() {
DevicePolicyManager devicePolicyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
return devicePolicyManager.isProfileOwnerApp("com.google.android.apps.work.clouddpc");
}
Erkennen, ob das Gerät ein Arbeitsprofil hat
So können Sie feststellen, ob ein Gerät ein Arbeitsprofil hat, das von der Android Device Policy App verwaltet wird: Diese Funktion kann in jedem Verwaltungsmodus aufgerufen werden. Wenn in einer App im privaten Profil nach dem Intent com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE gesucht wird, sollte dies als profilübergreifender Intent aufgelöst werden, wenn ein von der Android Device Policy App verwaltetes Arbeitsprofil vorhanden ist. Diese Methode gibt nur true zurück, wenn sie über das private Profil eines Geräts mit einem solchen Arbeitsprofil aufgerufen wird.
Android 11 und höher:
Kotlin
fun hasWorkProfile(): Boolean {
val intent = Intent("com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE")
val activities = context.packageManager.queryIntentActivities(intent, 0)
return activities.any { it.isCrossProfileIntentForwarderActivity }
}
Java
boolean hasWorkProfile() {
Intent intent = new Intent("com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE");
List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
return activities.stream()
.anyMatch(
(ResolveInfo resolveInfo) -> {
return resolveInfo.isCrossProfileIntentForwarderActivity();
});
}
Vor Android 11:
Kotlin
fun hasWorkProfile(): Boolean {
val intent = Intent("com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE")
val activities = context.packageManager.queryIntentActivities(intent, 0)
return activities.any { it.activityInfo.name == "com.android.internal.app.ForwardIntentToManagedProfile" }
}
Java
boolean hasWorkProfile() {
Intent intent = new Intent("com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE");
List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
return activities.stream()
.anyMatch(
(ResolveInfo resolveInfo) -> {
return resolveInfo.activityInfo.name.equals("com.android.internal.app.ForwardIntentToManagedProfile");
});
}