Android Multi-User

本文介绍了如何检查当前用户是否为所有者,判断进程是否运行在当前用户下,以及如何确认进程是否在管理型配置(Android for Work)或访客用户中运行。涉及到的关键权限包括INTERACT_ACROSS_USERS和MANAGE_USERS。同时,文章通过反射方式讨论了单用户属性的生效条件,以及后台用户启动活动的行为。最后,提到了在SHEP/HEP上启用多用户功能的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to check whether current user is owner

boolean isOwner = false;
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
if (um != null) {
    UserHandle userHandle = android.os.Process.myUserHandle();
    isOwner = um.getSerialNumberForUser(userHandle) == 0;
}

How to check whether the process runs on current user

It will need to use permission INTERACT_ACROSS_USERS and MANAGE_USERS (both are signature|system) for getCurrentUser.

boolean isCurrentUser = false;
try {
    Method getUhId = UserHandle.class.getDeclaredMethod("getIdentifier");
    Method getCuId = ActivityManager.class.getDeclaredMethod("getCurrentUser");
    Integer myUhId = (Integer) getUhId.invoke(android.os.Process.myUserHandle());
    Integer cuId = (Integer) getCuId.invoke(null);
    isCurrentUser = myUhId != null && myUhId.equals(cuId);
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
         | InvocationTargetException e) {
    // ...
}

How to check whether the process runs on managed profile (Android for Work) or guest user

It will need to use permission MANAGE_USERS (signature|system).

UserManager um = UserManager.get(context);
boolean isManagedProfile = um.isManagedProfile();
boolean isGuestUser = um.isGuestUser();

By reflection:

try {
    Method getUM = UserManager.class.getDeclaredMethod("get", Context.class);
    Method isMP = UserManager.class.getDeclaredMethod("isManagedProfile");
    Method isGU = UserManager.class.getDeclaredMethod("isGuestUser");
    UserManager um = (UserManager) getUM.invoke(null, context);
    Boolean isManagedProfile = (Boolean) isMP.invoke(um);
    Boolean isGuestUser = (Boolean) isGU.invoke(um);
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}

Effective criteria of singleUser attribute for component This flag can only be used with services, receivers, and providers; it can not be used with activities.
Need permission android.permission.INTERACT_ACROSS_USERS (system|signature).
Need to satisfy one of the below conditions appId >= 10000 (appId means base uid that stripping out the user id, see slide page 19) appId is PHONE_UID (1001)
Declared android:persistent=”true”

Single user component constraint
exported will be false, only put in priv-app can set to true.
It will need to declare android.permission.INTERACT_ACROSS_USERS_FULL (platform key signature) to allow other application access it. reference

Broadcast send from system uid will ignore singleUser attribute, which means the receiver will have different processes on each running users.

Background user launches activity

According to 93529a4, 4f1df4fa.
The last focused stack will be remembered when the user changes. It will restore that stack when the user changes back.

So if user 0 is on home activity (home stack), then switch to user 10. A component in user 0 starts an activity X (in app stack). Switch back to user 0 will see home. The activity X will be created when its task becomes top task.
If user 0 switch to user 10 when user 0’s top is an application (app stack), then A component in user 0 starts an activity X, switch back to user 0 will see the activity X.

Steps of enable multi-user on SHEP/HEP (for local test)

adb remount
adb shell "echo 'fw.max_users=3' >> /system/build.prop"
adb shell "echo 'fw.show_multiuserui=1' >> /system/build.prop"
adb reboot
Adding and switching users from Settings > Users 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值