Erkennung von Arbeitsprofilen

In dieser Anleitung wird gezeigt, wie Sie ein Arbeitsprofil auf einem Gerät erkennen. Sie gilt nur für Arbeitsprofile, die über die Android Device Policy App verwaltet werden.

Erkennen, ob die App in einem Arbeitsprofil ausgeführt wird

Bei der folgenden Methode wird geprüft, ob die anrufende 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");
}

Prüfen, 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 Sie von einer App im privaten Profil eine Abfrage für den Intent com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE stellen, sollte dieser als profilübergreifender Intent aufgelöst werden, wenn ein Arbeitsprofil vorhanden ist, das von der Android Device Policy App verwaltet wird. Diese Methode gibt nur dann 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");
            });
}