zoukankan      html  css  js  c++  java
  • 服务管理类-----ActivityManager

    ndroid.app.ActivityManager

    这个类主要用来管理所有设备上的Activities。

    权限:android.permission.GET_TASKS

    方法:| 返回类型     方法|

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

      返回用户最近使用过的应用程序信息集合。第一个参数是最大数量,第二个参数在API11前只有ActivityManager.RECENT_WITH_EXCLUDED。这个方法以后可能会被废弃,谷歌推荐用来debug用。

       (1) RecentTaskInfo类,主要有些字段

        1.1 Intent      baseIntent   得到能跳转到这个Task的Activity,即能通过这个Intent启动这个Intent指向的程序。

        1.2  int           id                    得到这个Task的标识,如果是-1,则标识这个程序没启动,其他数字表示启动了。

        1.3  int           persistentId  任务的唯一值。

        1.4  Intent     baseIntent    启动任务的Intent

       任务栏(长按Home键或者Menu键或者点击任务键)里面的任务主要就是靠这个方法获取的。

    1. final List<ActivityManager.RecentTaskInfo> recentTasks =  
    2.         am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);  

      点击启动任务

    1. if (ad.taskId >= 0) {  
    2.     // This is an active task; it should just go to the foreground.  
    3.     am.moveTaskToFront(ad.taskId, ActivityManager.MOVE_TASK_WITH_HOME,  
    4.             opts);  
    5. else {  
    6.     Intent intent = ad.intent;  
    7.     intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY  
    8.             | Intent.FLAG_ACTIVITY_TASK_ON_HOME  
    9.             | Intent.FLAG_ACTIVITY_NEW_TASK);  
    10.     if (DEBUG) Log.v(TAG, "Starting activity " + intent);  
    11.     context.startActivityAsUser(intent, opts,  
    12.             new UserHandle(UserHandle.USER_CURRENT));  
    13. }  

      滑动删除任务(hide方法,需系统权限android.permission.REMOVE_TASKS)

    1. am.removeTask(ad.persistentTaskId, ActivityManager.REMOVE_TASK_KILL_PROCESS);  

      设置中强行停止应用(hide,需要非public权限)

    1. ActivityManager am = (ActivityManager)getActivity().getSystemService(  
    2.         Context.ACTIVITY_SERVICE);  
    3. am.forceStopPackage(pkgName);  

    2.List<ActivityManager.RunningAppProcessInfo>           getRunningAppProcesses()

       返回设备上正在运行的程序的进程集合。

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

         1.1 int     importance   这个字段的值如果为IMPORTANCE_FOREGROUND (100)时表示为前段运行的进程,为IMPORTANCE_BACKGROUND(400)表示后台运行。其值还有一些其他的。

         1.2 int     importanceReasonCode  对进程进行解释的字段,有3个值 REASON_UNKNOWN(0) 、REASON_PROVIDER_IN_USE(1)、REASON_SERVICE_IN_USE(2)。分别表示没原因、程序中的content provider被其他程序使用、与前一个理由一样。

         1.3 int    importanceReasonPid   当 importanceReasonCode  的值不为0时这个字段代表1.2中其他程序的PID值,否则值为0。

         1.4 ComponentName     importanceReasonComponent     当 importanceReasonCode  中值不为0时,这个字段代表1.2中其他程序的 ComponentName,否则为null。

         1.5 int     pid    进程的PID值。

         1.6 int   uid    user id.

         1.7  String    processName   进程名,实际等于包名(content provider免疫)。

         1.8 String[]     pkgList    程序内所有主包,这个测试出来,一般程序都只有一个元素,但系统自带的程序并且有content provider的有几个包。

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

         返回正在运行中的程序,参数为返回的最大个数,返回的顺序为  最近打开的程序,即优先返回最新使用的程序。返回值可能为空。

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

         1.1 ComponentName        baseActivity    代表登陆的Activity的 ComponentName,即可以用这个返回值启动这个所代表的程序。

         1.2  int               id                    A unique identifier for this task.

         1.3  int         numActivities     Number of activities in this task.

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

         1.5 ComponentName         topActivity    存在栈顶的Activity所代表的 ComponentName。用户可以用这个方法得到现在正在显示的Activity的 ComponentName。

        

    1. ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
    2. 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类,主要提供字段。

       1.1  long       activeSince    第一次启动这个service到现在所过的时间段。

       1.2  int           pid              进程的PID

       1.3  int   uid    user id.

       1.4  String      process    进程名(包名?)。

       1.5  long     lastActivityTime  最后一次激活Service到现在的时间

       1.6 ComponenName     service    得到这个Service的组件名,就是能通过这个启动service

       1.7  long   restarting     这个字段的值如果不是0,那么现在这个service还没启动,将在返回值的时间段过后自启动。

    6. void    restartPackage (String packageName)

         现在这个方法等于方法4了,调用这个方法等于在调用方法4.

  • 相关阅读:
    python与数据库(SQLServer、MySQL、Redis、MongoDB)
    网络表示学习 图神经网络资料整理
    Java学习笔记 算法 Algorithms Fourth Edition
    word2vec 代码解读
    爬虫python的世界
    mac 字幕生成工具
    CS224W Analysis of Networks
    SNAP macos
    python基础代码
    Keras笔记
  • 原文地址:https://www.cnblogs.com/DASOU/p/4186673.html
Copyright © 2011-2022 走看看