[Android] Android ActivityManager

android.app.ActivityManagerjava

這個類主要用來管理全部設備上的Activities。android

權限:android.permission.GET_TASKSapp

1.List<ActivityManager.RecentTaskInfo>       getRecentTasks(int maxNum, int flags)  ide

  返回用戶最近使用過的應用程序信息集合。第一個參數是最大數量,第二個參數在API11前只有ActivityManager.RECENT_WITH_EXCLUDED。這個方法之後可能會被廢棄,谷歌推薦用來debug用。測試

   (1) RecentTaskInfo類,主要有些字段ui

     Intent      baseIntent   獲得能跳轉到這個Task的Activity,即能經過這個Intent啓動這個Intent指向的程序。this

     int           id                    獲得這個Task的標識,若是是-1,則標識這個程序沒啓動,其餘數字表示啓動了。spa

2.List<ActivityManager.RunningAppProcessInfo>           getRunningAppProcesses()debug

   返回設備上正在運行的程序的進程集合。rest

   (1)RunningAppProcessInfo類,主要有些字段

      int     importance   這個字段的值若是爲IMPORTANCE_FOREGROUND (100)時表示爲前段運行的進程,爲IMPORTANCE_BACKGROUND(400)表示後臺運行。其值還有一些其餘的。

     int     importanceReasonCode  對進程進行解釋的字段,有3個值 REASON_UNKNOWN(0) 、REASON_PROVIDER_IN_USE(1)、REASON_SERVICE_IN_USE(2)。分別表示沒緣由、程序中的content provider被其餘程序使用、與前一個理由同樣。

     int    importanceReasonPid   當 importanceReasonCode  的值不爲0時這個字段表明1.2中其餘程序的PID值,不然值爲0。

     ComponentName     importanceReasonComponent     當 importanceReasonCode  中值不爲0時,這個字段表明1.2中其餘程序的 ComponentName,不然爲null。

     int     pid    進程的PID值。

     int   uid    user id.

     String    processName   進程名,實際等於包名(content provider免疫)。

     String[]     pkgList    程序內全部主包,這個測試出來,通常程序都只有一個元素,但系統自帶的程序而且有content provider的有幾個包。

3 List<ActivityManager.RunningTaskInfo>        getRunningTasks(int maxNum)

     返回正在運行中的程序,參數爲返回的最大個數,返回的順序爲  最近打開的程序,即優先返回最新使用的程序。返回值可能爲空。

   (1)RunningTaskInfo類,主要提供字段

      ComponentName        baseActivity    表明登錄的Activity的 ComponentName,便可以用這個返回值啓動這個所表明的程序。

      int               id                    A unique identifier for this task.

      int         numActivities     Number of activities in this task.

        int          numRunning         Number of activities that are currently running (not stopped and persisted) in this task.

       ComponentName         topActivity    存在棧頂的Activity所表明的 ComponentName。用戶能夠用這個方法獲得如今正在顯示的Activity的 ComponentName。

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;


    


 4.void    killBackgroundProcesses (String packageName)

    殺死後臺進程,參數爲要被殺的程序的(主)包名。須要權限android.permission.KILL_BACKGROUND_PROCESSES。

5. List<ActivityManager.RunningServiceInfo>           getRunningServices(int maxNum) 

      獲得全部正在運行的service。

  (1)RunningServiceInfo類,主要提供字段。

    long       activeSince    第一次啓動這個service到如今所過的時間段。

    int           pid              進程的PID

    int   uid    user id.

   String      process    進程名(包名?)。

   long     lastActivityTime  最後一次激活Service到如今的時間

   ComponenName     service    獲得這個Service的組件名,就是能經過這個啓動service

    long   restarting     這個字段的值若是不是0,那麼如今這個service還沒啓動,將在返回值的時間段事後自啓動。

6. void    restartPackage (String packageName)

     如今這個方法等於方法killBackgroundProcesses了,調用這個方法等於在調用方法killBackgroundProcesses.須要權限android.permission.RESTART_PACKAGES。

相關文章
相關標籤/搜索