How to check if "Multiple users" is enabled

651 views Asked by At

Is there a system setting table or API I can check to see if the "Multiple users" setting is turned on in Settings -> System -> Advanced -> Multiple users?

enter image description here

Thanks!

2

There are 2 answers

0
Flyview On BEST ANSWER

After a few hours of searching, I found the answer by browsing /data/system/users/0 and looking through settings_system.xml, settings_secure.xml, and settings_global.xml. It turns out what I'm looking for is "user_switcher_enabled" in settings_global.xml.

Here's my code:

public static boolean isMultipleUsersEnabled(Context context) {
    try {
        int pref = Settings.Global.getInt(context.getContentResolver(), "user_switcher_enabled");
        return pref == 1 && (Build.VERSION.SDK_INT < Build.VERSION_CODES.N || UserManager.supportsMultipleUsers());
    } catch (Settings.SettingNotFoundException e) {
        Utils.logError(TAG, "user_switcher_enabled setting not found: " + e.toString());
        return false;
    } 
}

The check for UserManager.supportsMultipleUsers() is probably not needed, but just in case the preference is somehow bogus on devices that don't actually support multiple users.

If there's anything you want to add to this answer, please comment below.

1
Yong On

For API level 24 and above, you can use the method UserManager.supportsMultipleUsers(), which returns whether the device supports multiple users in boolean.

Before API level 24, there are no methods to check this without system permissions. There can be a workaround, like getting the count of users using the method getUserCount(). Again this also needs android.permission.MANAGE_USERS permission.