Process#myUid
/**
* Returns the identifier of this process's uid. This is the kernel uid
* that the process is running under, which is the identity of its
* app-specific sandbox. It is different from {@link #myUserHandle} in that
* a uid identifies a specific app sandbox in a specific user.
*/
public static final int myUid() {
return Os.getuid();
}
Process#myUserHandle
/**
* Returns this process's user handle. This is the
* user the process is running under. It is distinct from
* {@link #myUid()} in that a particular user will have multiple
* distinct apps running under it each with their own uid.
*/
public static UserHandle myUserHandle() {
return UserHandle.of(UserHandle.getUserId(myUid()));
}
UserHandle#getUserId
/**
* @hide Range of uids allocated for a user.
*/
public static final int PER_USER_RANGE = 100000;
/** @hide A user id constant to indicate the "system" user of the device */
public static final @UserIdInt int USER_SYSTEM = 0;
/**
* Returns the user id for a given uid.
* @hide
*/
public static @UserIdInt int getUserId(int uid) {
if (MU_ENABLED) {
return uid / PER_USER_RANGE;
} else {
return UserHandle.USER_SYSTEM;
}
}
从上面可以知道Process#myUid返回的值有点类似Pid,就是每个进程都不一样。
而UserHandle#getUserId返回的值,就是多用户系统中代表某个用户的值。而这个值可以从Process#myUid返回值除以一个10000得到。就是说理论上,每个用户可以同时运行10000个进程。