במדריך הזה מוסבר איך לזהות פרופיל עבודה במכשיר. ההגדרה חלה רק על פרופילי עבודה שמנוהלים על ידי אפליקציית Android Device Policy.
זיהוי אם האפליקציה פועלת בתוך פרופיל עבודה
השיטה הבאה בודקת אם האפליקציה שקוראת פועלת בפרופיל עבודה שמנוהל על ידי אפליקציית Device Policy למכשירי Android.
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");
}
זיהוי אם במכשיר יש פרופיל עבודה
כדי לבדוק אם במכשיר יש פרופיל עבודה שמנוהל על ידי אפליקציית Device Policy למכשירי Android, משתמשים בשיטה הבאה. אפשר לקרוא לשיטה הזו מכל מצב ניהול. מתוך אפליקציה בפרופיל האישי, שאילתה לגבי הכוונה com.google.android.apps.work.clouddpc.ACTION_DETECT_WORK_PROFILE אמורה להניב כוונה חוצת-פרופילים אם קיים פרופיל עבודה שמנוהל על ידי אפליקציית Android Device Policy. ה-method הזו תחזיר true רק אם היא תופעל מתוך הפרופיל האישי של מכשיר שיש בו פרופיל עבודה.
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.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();
});
}
בגרסאות קודמות ל-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");
});
}