您的位置:首页 > 移动开发 > Android开发

Android Multi-User

2015-07-03 16:19 771 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: