Android - Get list of users and profile name

50.7k views Asked by At

We can create several users and profiles on an Android devices, for example, there is a Main User, and I also set a Work Profile with name "Work Profile".

Using the following adb command:

adb shell pm list users

I'm getting this result:

UserInfo{0:My User:13} running UserInfo{10:Work Profile:30} running

Good, I can see my User Info with the name Work Profile on it. Now, I want to get this list from my application.

I tried to use this code:

for(Account account: AccountManager.get(this).getAccounts()) {
        DebugLog.e("COUCOU " + account.name + " " + account.toString());
    }

But the Work Profile account is not appearing in the list. Only the main user... Any idea how I could get the same list as the ADB shell command??

2

There are 2 answers

0
shalafi On BEST ANSWER

You are confusing users with accounts. What you are using will only get you the accounts of the current user.

A device can have multiple users, which are the equivalent of users on a computer.

A user can have multiple accounts, i.e., let that be Gmail, Facebook, Twitter, etc. The accounts may be shared among users or not.

For more info on that topic, check out the official page on Supporting Multiple Users.

You can handle users with UserManager and you can get the list of the profiles of the current user using getUserProfiles, but what you'll get is a UserHandle (which you can use to get the userId), how to get the list of all users and the name is something I haven't found out yet, but on the worst case you can use the shell command pm, as shown in this post about Multiple user accounts on Android.

0
ppoffice On

If you have the root access to your device, or you are able to build your app as a system privileged application, you can use Privileged Permission Whitelisting to obtain the android.permission.MANAGE_USERS permission (which also requires you to sign your app with the platform signature). Then, you can get a list of all UserInfos by calling UserManager.getUsers() using Java reflection.