How to get UserHandle of activity in multi user system
Asked Answered
M

2

9

I have a service that runs in the background that monitors the foreground activity. At present this does not distinguish between users (multi user accounts Android 4.2). Is there a way that the service can compare the user it is running under with that of the foreground activity? I am only interested in activities of the same user account that the service is running under.

I know I can use:

android.os.Process.myUserHandle()

to get the UserHandle that the service is running under. But I can't see anything in the docs that can give me the UserHandle of a running activity (ActivityManager.RunningAppProcessInfo etc).

Any ideas?

Mesopause answered 5/8, 2013 at 9:17 Comment(0)
B
7

Activity always runs as user returned by android.os.Process.myUserHandle().

Barfield answered 13/1, 2016 at 21:42 Comment(0)
G
4

From inside the service, you can use android.os.Binder.getCallingUid() to get the full kernel uid of the process containing the Activity that's calling the service. Run this result through android.os.Process.getUserId() to get the two digit Android user handle and compare it with android.os.Process.myUserHandle().

Greatniece answered 24/7, 2014 at 15:6 Comment(1)
You cannot convert UID to userId using public API. UserHandle.getUserId is not public.Barfield

© 2022 - 2024 — McMap. All rights reserved.