1. 程式人生 > >14 展訊Sprd設定-電池-監聽程序狀態變化方法 PROCESS_STATE

14 展訊Sprd設定-電池-監聽程序狀態變化方法 PROCESS_STATE

1. 程序狀態列表

  • frameworks/base/core/java/android/app/ActivityManager.java
    程序狀態,也可以表徵程序的優先順序adj,讓系統自動進行記憶體回收,同樣也可以作為程序管理的參考引數
    /** @hide Not a real process state. */
    public static final int PROCESS_STATE_UNKNOWN = -1;

    /** @hide Process is a persistent system process. */
    public static final int PROCESS_STATE_PERSISTENT = 0;

    /** @hide Process is a persistent system process and is doing UI. */
    public static final int PROCESS_STATE_PERSISTENT_UI = 1;

    /** @hide Process is hosting the current top activities.  Note that this covers
     * all activities that are visible to the user. */
    public static final int PROCESS_STATE_TOP = 2;

    /** @hide Process is hosting a foreground service due to a system binding. */
    public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 3;

    /** @hide Process is hosting a foreground service. */
    public static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;

    /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
    public static final int PROCESS_STATE_TOP_SLEEPING = 5;

    /** @hide Process is important to the user, and something they are aware of. */
    public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 6;

    /** @hide Process is important to the user, but not something they are aware of. */
    public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 7;

    /** @hide Process is in the background transient so we will try to keep running. */
    public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 8;

    /** @hide Process is in the background running a backup/restore operation. */
    public static final int PROCESS_STATE_BACKUP = 9;

    /** @hide Process is in the background, but it can't restore its state so we want
     * to try to avoid killing it. */
    public static final int PROCESS_STATE_HEAVY_WEIGHT = 10;

    /** @hide Process is in the background running a service.  Unlike oom_adj, this level
     * is used for both the normal running in background state and the executing
     * operations state. */
    public static final int PROCESS_STATE_SERVICE = 11;

    /** @hide Process is in the background running a receiver.   Note that from the
     * perspective of oom_adj receivers run at a higher foreground level, but for our
     * prioritization here that is not necessary and putting them below services means
     * many fewer changes in some process states as they receive broadcasts. */
    public static final int PROCESS_STATE_RECEIVER = 12;

    /** @hide Process is in the background but hosts the home activity. */
    public static final int PROCESS_STATE_HOME = 13;

    /** @hide Process is in the background but hosts the last shown activity. */
    public static final int PROCESS_STATE_LAST_ACTIVITY = 14;

    /** @hide Process is being cached for later use and contains activities. */
    public static final int PROCESS_STATE_CACHED_ACTIVITY = 15;

    /** @hide Process is being cached for later use and is a client of another cached
     * process that contains activities. */
    public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 16;

    /** @hide Process is being cached for later use and is empty. */
    public static final int PROCESS_STATE_CACHED_EMPTY = 17;

    /** @hide Process does not exist. */
    public static final int PROCESS_STATE_NONEXISTENT = 18;

2. 監聽程序狀態變化方法

註冊 UID_OBSERVER_PROCSTATE 的監聽器即可

    private void registerUidObserver() {
        try {
            if (mActivityManager != null)
                mActivityManager.registerUidObserver(mUidObserver, ActivityManager.UID_OBSERVER_PROCSTATE
                  | ActivityManager.UID_OBSERVER_GONE
                  /*| ActivityManager.UID_OBSERVER_IDLE
                  | ActivityManager.UID_OBSERVER_ACTIVE*/,
                  ActivityManager.PROCESS_STATE_UNKNOWN, null);
        } catch (RemoteException e) {
            // ignored; both services live in system_server
        }
    }

    final private IUidObserver mUidObserver = new IUidObserver.Stub() {
        @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) throws RemoteException {
            synchronized (mUidStateLock) {
                updateUidStateLocked(uid, procState);
            }
            mAppStateInfoCollector.updateUidState(uid, procState);
        }

        @Override public void onUidGone(int uid, boolean disabled) throws RemoteException {
            synchronized (mUidStateLock) {
                removeUidStateLocked(uid);
            }
            mAppStateInfoCollector.removeUidState(uid);
        }

        @Override public void onUidActive(int uid) throws RemoteException {
            //synchronized (mUidStateLock) {
            //    updateUidStateLocked(uid, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND);//?
            //}
        }


        @Override public void onUidIdle(int uid, boolean disabled) throws RemoteException {
            //synchronized (mUidStateLock) {
            //    removeUidStateLocked(uid);
            //}
        }

        @Override public void onUidCachedChanged(int uid, boolean cached) throws RemoteException {
        }

    };