1. 程式人生 > >UserHandle#getUserId、Process#myUid、Process#myUserHandle的區別和聯絡

UserHandle#getUserId、Process#myUid、Process#myUserHandle的區別和聯絡

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個程序。

 

Java Examples for android.os.UserHandle