3

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 ?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85

1 Answers1

2

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();
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Unfortunately I don't think this works anymore, I tried. Firstly, now directories seem to be created in the order 0, 11, 12, 13, etc. And secondly they don't seem to be accessible without root. /data is protected. I am simply trying to find if multiple users is enabled or not: https://stackoverflow.com/questions/67629534/how-to-check-if-multiple-users-is-enabled – Flyview May 21 '21 at 06:36