When a device supports multiple users, its apps must be made aware of these distinct users.
Certain apps need to have some components run as singletons and can accept requests from any user. Only system apps can currently use this feature.
- Conserves resources
- Arbitrates one or more shared resources across users
- Reduces network overhead by using a single server connection
See the diagram below for a depiction of permissions flow with multiple users.
Enabling a singleton component
To identify an app as a singleton, Add
android:singleUser=”true” to your service or provider in the Android manifest.
The system will instantiate that component in the process running as user 0 only. Any requests to connect to that provider or service from any user will be routed to the process in user 0. If this is the only component in your app, only one instance of your app will run.
Activities in your package will still be launched in a separate process for each user, with the UID being in the UID range for that user (such as 1010034).
Interacting with users
These permissions are required
INTERACT_ACROSS_USERS (signature|system) INTERACT_ACROSS_USERS_FULL (signature)
Use the following APIs to make apps aware of multiple users.
- Extract the user handle from incoming Binder calls:
int userHandle = UserHandle.getCallingUserId()
- Use new, protected APIs to start services, activities, broadcasts on a specific
Context.bindServiceAsUser(Intent, …, UserHandle)
Context.sendBroadcastAsUser(Intent, … , UserHandle)
Context.startServiceAsUser(Intent, …, UserHandle) UserHandlecan be an explicit user or one of the special handles:
CURRENTindicates the user that is currently in the foreground. You can use
ALLwhen you want to send a broadcast to all users.
- Communicate with components in your own app:
(INTERACT_ACROSS_USERS)Or with components in other apps:
- You may need to create proxy components that run in the user’s process that
then access the
singleUsercomponent in user 0.
- Query users and their handles with the new
UserManager.getUserSerialNumber(int userHandle)- a non-recycled number that corresponds to a user handle.
UserManager.getUserProfiles()- returns the collection of self and managed profiles, if any.
- Register to listen to specific or all users and the callbacks with new APIs on ContentObserver, PackageMonitor, BroadcastReceiver that provide additional information about which user has caused the callback.