Android for work - How to check if my application is running in the work profile?
Asked Answered
S

2

16

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 for work documentation: https://developer.android.com/training/enterprise/index.html

Stinkpot answered 25/5, 2015 at 16:6 Comment(2)
Did you manage to identify when an app is running on work profile or not?Horthy
Unfortunately no. If you find anything, please let me know.Stinkpot
P
24

I found a solution : if isProfileOwnerApp return true for one package name, it means that your app (badged) is running on work profile. if your app is running in normal mode (no badge) isProfileOwnerApp return false for all admins even if there is a Work profile.

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));
   }
}
Pigeonhole answered 19/11, 2015 at 10:23 Comment(3)
How do you know it's a work profile? or are all profiles work profiles?Hoick
@Hoick if (devicePolicyManager.isProfileOwnerApp(packageName)) return true; inside the loop, if one package return true, then you are in work profile. Otherwise, you are not.Potbellied
in my case i am getting both false in Xiaomi and activeadmins null in one plus, i am using Island app for work profile.Menzies
F
2

I took the response from @earlypearl into a Kotlin class:

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
        }
}
Forrester answered 11/2, 2021 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.