זיהוי של פרופיל העבודה

במדריך הזה מוסבר איך לזהות פרופיל עבודה במכשיר. ההגדרה הזו חלה רק על פרופילי עבודה שמנוהלים על ידי אפליקציית Android Device Policy.

זיהוי אם האפליקציה פועלת בתוך פרופיל העבודה

השיטה הבאה בודקת אם אפליקציית הקריאה פועלת בתוך פרופיל עבודה שמנוהל על ידי אפליקציית Device Policy ל-Android.

KotlinJava
fun isInsideWorkProfile(): Boolean {
  val devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager

  return devicePolicyManager.isProfileOwnerApp("com.google.android.apps.work.clouddpc")
}
boolean isInsideWorkProfile() {
  DevicePolicyManager devicePolicyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

  return devicePolicyManager.isProfileOwnerApp("com.google.android.apps.work.clouddpc");
}

זיהוי אם במכשיר יש פרופיל עבודה

כדי לקבוע אם במכשיר יש פרופיל עבודה שמנוהל על ידי אפליקציית Android Device Policy, משתמשים בשיטה הבאה. אפשר להפעיל את הפונקציה הזו מכל מצב ניהול. אם יש פרופיל עבודה שמנוהל על ידי אפליקציית Android Device Policy, שאילתה על הכוונה com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE מאפליקציה בפרופיל האישי אמורה לפתור ככוונה בכמה פרופילים. השיטה הזו תחזיר את הערך true רק כשקוראים לה מהפרופיל האישי של מכשיר שיש לו פרופיל עבודה כזה.

Android מגרסה 11 ואילך:

KotlinJava
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 }
}
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();
            });
}

לפני Android 11:

KotlinJava
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" }
}
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");
            });
}