3
votes

Android 4.2 & above supports Multiple Users as described at below link...

http://developer.android.com/about/versions/android-4.2.html#MultipleUsers

Now, my questions are..

  1. Is it possible to know multiple user configured in device programmatically ? If yes, then how to get number of user configured in device ?

  2. Is it possible to get list of users ? if yes then how ?

1

1 Answers

2
votes

There is a public API to get user details but it is restricted to apps which have the "android.permission.MANAGE_USERS" permission. This permission has a protection level of "signature|system", so unless your app is installed to the system partition there is no official way to get the number of users.

I looked over the source code and came up with the following solution:

public int getNumUsers() {
    // The user directory created by android.server.pm.UserManagerService
    File userDir = new File(Environment.getDataDirectory(), "user");

    // Get the number of maximum users on this device.
    int id = Resources.getSystem()
            .getIdentifier("config_multiuserMaximumUsers", "integer", "android");
    int maxUsers;
    if (id != 0) {
        maxUsers = Resources.getSystem().getInteger(id);
    } else {
        maxUsers = 5;
    }

    int numUsers = 0;
    for (int i = 0; i <= maxUsers * 10; i += 10) {
        // If a user is created, a directory will be created under /data/user in increments
        // of 10. The first user will always be 0.
        if (new File(userDir, Integer.toString(i)).exists()) {
            numUsers++;
        }
    }

    return numUsers;
}

It has worked on the brief testing I have done but it is not used in any production code. It needs more testing but should work.


If your app had system level permissions this would be much easier:

UserManager userManager = (UserManager) getSystemService(USER_SERVICE);
int userCount = userManager.getUserCount();