Can someone explain the AsUser methods?

1,885 views
Skip to first unread message

satur9nine

unread,
Jun 26, 2018, 10:13:40 AM6/26/18
to android-platform
I'm trying to understand the behavior of the following methods with regards to the UserHandle parameter:

  • startServiceAsUser
  • sendBroadcastAsUser
  • registerReceiverAsUser
  • startActivityAsUser
  • bindServiceAsUser

As a developer of services which run as system in the platform I'd like to know how to determine what the proper UserHandle parameter should be. If the non "AsUser" method is invoked I am given the following rebuke: "Calling a method in the system process without a qualified user"

It seems that sending a broadcast with UserHandle.ALL will cause that broadcast to go to all receivers regardless of the current user. Whereas using UserHandle.CURRENT the broadcast will be sent only to the current user. But what about sending a broadcast as UserHandle.SYSTEM?

I'm even less certain how startServiceAsUser or startActivityAsUser behaves with the different possible values, it seems to me that UserHandle.ALL makes no sense in this circumstance but what about UserHandle.SYSTEM vs UserHandle.CURRENT for starting activities and services?

How can I determine which UserHandle I should be using as a developer of system apps?

Thanks,
Jacob

Elangovan Manickam

unread,
Mar 7, 2019, 10:48:47 AM3/7/19
to android-platform
Hi jacob,

Did you find answer for your query? I'm also having the same queries but having hard time to find answer. 

Please share if you have got it cleared.

Thanks,
Elan

satur9nine

unread,
Mar 14, 2019, 1:26:31 PM3/14/19
to android-platform
I never got a satisfactory answer. The function UserController#handleIncomingUser in frameworks/base is a little helpful (and flippant) in understanding what is allowed, but it would be nice to get a clear answer from someone at Google as to when exactly each type of UserHandle should be used.

Jacob

Abhishek Subal

unread,
May 7, 2019, 3:12:02 PM5/7/19
to android-platform
Hello Jacob,
AsUser Methods were introduced in AOSP when Android started supporting Multiple Users on the Android (extensively used by Enterprises for use cases like implementing BYOD).
"As developer of services which run as system", you would want to know exactly for which user, you are trying to do a particular thing like starting an Activity.
Like if you are starting an Activity for KeyGuard (Lock Screen), you might want to start that activity as a particular user (different users having different lock screens and security settings).
So determining the correct UserHandle depends on the question , is your system service doing things differently for different users.
UserHandle.SYSTEM should not be confused with UserHandle.CURRENT because CURRENT refers to currently active user. 
When you setup an Android device for the first time and complete the setup wizard of the phone a USER  is created with UserID (different from uid in case of android) 0, which is same as UserHandle.System, but if some other Users were created afterwards on the device, then UserHandle.CURRENT may be different from UserHandle.SYSTEM (FYI UserHandle.SYSTEM replaces UserHandle.OWNER because historically first user created on Android device is owner of the device and the owner could create or delete other users, but as multi-user support on Android is growing with every release, the authority of adding or removing other users now may not always be with the first user created on the device that's why they have changed the name from UserHandle.OWNER to UserHandle.SYSTEM which makes more sense according to growing multi-user capabilities of Android).


Abhishek Subal 



Reply all
Reply to author
Forward
0 new messages