4

I am having Nexus 7 running multiple users. I am trying to find out which is currently running user using ADB. The best I could get so far was to check legacy directory linking which is either linked to /mnt/shell/emulated/0 or to /mnt/shell/emulated/1.

Is there more efficient way to find out which is currently running user? Also is there a way to find out user name of current user?

10

For Android Oreo and possibly for Nougat too I suppose, run:

adb shell get-current-user  #returns USER_ID of the foreground user 

Alternatively, do:

adb shell dumpsys activity | grep mCurrentUser

You output would be:

mCurrentUser=0

0 is always the user Id for the owner (primary user). Any other number, say 10 or 11, would reflect a secondary user (including Guest user).

For Lollipop, enter the command:

adb shell dumpsys user

You would get something like:

Users:
  UserInfo{0:Owner_lol:13} serialNo=0
    Created: 
    Last logged in: +1m40s308ms ago
  UserInfo{10:New user:10} serialNo=13
    Created: +1h1m15s438ms ago
    Last logged in: +6m2s390ms ago

In {0:Owner_lol:13} 0 is the userID of the user having user name Owner_lol.

For each user you would see Last logged in: time. The entry having time closest to current time implies that the corresponding user is currently in focus on screen.

Given that you've root access, you can see all these details from the XML file of each of the user available under /data/system/users.

It is another matter that my Android 4.2.1 didn't give Last Logged in: time correctly.


Alternatively, in Lollipop, you can do:

adb shell pm list users

You would get something like:

Users:
        UserInfo{0:Owner_lol:13} running
        UserInfo{10:New user:10} 

In my Android 4.2.1 I don't see the text "running", perhaps because it wasn't implemented in the code or because the device has single-user support.

Note that if multiple users are logged in then you would see "running" for all the logged-in users.


user (android.os.IUserManager) is the service which manages users in the system. dumspsys simply dumped the output queried from the service user.

You can alternatively query the service provided that you know which function to query. For e.g. In my Android 5.0.2 I can enter:

adb shell service call user 8

It gives me a hex-dump where I can at least see the user name and userID among other things, of all the users in system.

Number 8 is calling a particular function. See the source code of android.os.IUserManager for your Android release here.

Edit:

An another method is to peek into the activity service.

adb shell dumpsys activity

Look for the few lines after the line with text mStartedUsers:. You would see something like:

User #0: mState=RUNNING
User #10: mState=RUNNING

The numeral entry between # and : is the userID of a user.

1

I tried everything Firelord suggested, but non of it actually gave useful results. It always says that all users are "RUNNING", "Last logged in" doesn't update/reset when switching user accounts...

I ended up with this:

adb shell dumpsys activity | grep -E "mUserLru" | grep -Eo "[0-9]+\]$" | tr -d "]"

Returns the user id of the active user.

This works very well for me.

protected by Community Sep 28 '16 at 14:56

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.