当前位置:   article > 正文

Android中ActivityManager学习笔记(1)-MemoryInfo_activitymanager.memoryinfo

activitymanager.memoryinfo

 ActivityManager

      

  与系统中所有的Activity进行交互的类。官网一句话解释了这个东西,但是有点随意了。ActivityManager的作用很多,我们通过它获得内存信息,进程信息,还可以终止某个进程。当然啦,只能终止用户的进程,系统的进程是杀死不了的。下面我通过实例,一步一步学习它的具体作用,我也是刚学,不能从宏观的角度给大家解释清楚它的具体作用。希望通过程序一步一步的写下去,其每个方法的作用也就清楚了,从而我们就知道它能干什么啦。语言组织能力差,所以就用代码来解释吧。

        首先我们创建一个程序,在入口Activity里的onCreate()方法里,我们来创建ActivityManager.

 

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

       

从系统服务里得到我们的ActivityManger对象.在官方文档中,并没有提供构造方法,所以我们不能通过new的方式创建该对象,由于ActivityManager是核心服务,所以android从系统服务里得到它的实例。

 

获取手机的内存信息MemoryInfo

 

  1. MemoryInfo memInfo = new MemoryInfo();//存放内存信息的对象
  2. activityManager.getMemoryInfo(memInfo);//传入参数,将获得数据保存在memInfo对象中
  3. long availMem = memInfo.availMem/1000000;//可用内存
  4. boolean isLowMem = memInfo.lowMemory;//是否达到最低内存
  5. long threshold = memInfo.threshold/1000000;//临界值,达到这个值,进程就要被杀死
  6. long totalMem = memInfo.totalMem/1000000;//总内存
  7. Log.i(TAG, "avail:" + availMem + ",isLowMem:" + isLowMem + ",threshold:" + threshold + ",totalMem:" + totalMem);

 

在ActivityManager的getMemoryInfo的方法中需要传入一个MemoryInfo类,该类为ActivityManager内部类,手机的内存信息就保存在这个类中。然后通过MemoryInfo类中的方法,获得我们想要的信息。运行程序输出结果如下:


 

然后我们在调用一下清楚用户数据的方法:

 

activityManager.clearApplicationUserData()//清楚当前应用的用户数据

 

放在刚才获取内存的数据前面,我们看看清除当前应用的用户数据以后,我们的内存大小:

 



可以看出我们清除了1M的用户数据。

 

系统各个进程的信息RunningAppProcessInfo

 

  1. List<RunningAppProcessInfo> list1 = activityManager.getRunningAppProcesses();
  2. for(RunningAppProcessInfo info :list1){
  3. int importance = info.importance;//通过给进程标值来给进程分类,越重要的进程,那么数值就越低
  4. Log.i(TAG, "importance: "+importance);
  5. int importanceReasonCode = info.importanceReasonCode;//重要性的原因,也是通过数值去判断。
  6. Log.i(TAG, "importanceReasonCode: "+importanceReasonCode);
  7. ComponentName impoartanceReasonComponent = info.importanceReasonComponent;//重要性原因的组件,返回一个ComponentName的对象。
  8. Log.i(TAG, "impoartanceReasonComponent: "+impoartanceReasonComponent);
  9. int importanceReasonPid = info.importanceReasonPid;//其他客户端进程的进程id号,如果没有其他进程返回0
  10. Log.i(TAG, "importanceReasonPid : "+importanceReasonPid);
  11. int lastTrimLevel = info.lastTrimLevel;//提供给ComponentCallbacks2.onTrimMemory(int)方法的参数。上一次提交给进程的内饰水平
  12. Log.i(TAG, "lastTrimLevel: "+lastTrimLevel);
  13. int lru = info.lru;//在一个特殊的进程中,提供更精细密度的衡量值,目前只维护IMPORTANCE_BACKGROUND
  14. Log.i(TAG, "lru :"+lru);
  15. int pid = info.pid;//进程id
  16. Log.i(TAG, "pid :"+pid);
  17. String proName = info.processName;//进程名
  18. Log.i(TAG, "proName : " + proName);
  19. int uid = info.uid;//用户id
  20. Log.i(TAG, "uid : " + uid);
  21. Log.i(TAG, "================================"); }

 

运行程序得到输出,截取一部分如下:

 

  1. 03-27 14:24:54.460: I/MainActivity(7647): importance: 100
  2. 03-27 14:24:54.460: I/MainActivity(7647): importanceReasonCode: 0
  3. 03-27 14:24:54.460: I/MainActivity(7647): impoartanceReasonComponent: null
  4. 03-27 14:24:54.460: I/MainActivity(7647): importanceReasonPid : 0
  5. 03-27 14:24:54.460: I/MainActivity(7647): lastTrimLevel: 0
  6. 03-27 14:24:54.460: I/MainActivity(7647): lru :0
  7. 03-27 14:24:54.460: I/MainActivity(7647): pid :7647
  8. 03-27 14:24:54.460: I/MainActivity(7647): proName : com.ibm.activitymanager
  9. 03-27 14:24:54.470: I/MainActivity(7647): uid : 10072
  10. 03-27 14:24:54.470: I/MainActivity(7647): ================================
  11. 03-27 14:24:54.470: I/MainActivity(7647): importance: 130
  12. 03-27 14:24:54.470: I/MainActivity(7647): importanceReasonCode: 0
  13. 03-27 14:24:54.470: I/MainActivity(7647): impoartanceReasonComponent: null
  14. 03-27 14:24:54.470: I/MainActivity(7647): importanceReasonPid : 0
  15. 03-27 14:24:54.480: I/MainActivity(7647): lastTrimLevel: 0
  16. 03-27 14:24:54.480: I/MainActivity(7647): lru :0
  17. 03-27 14:24:54.480: I/MainActivity(7647): pid :836
  18. 03-27 14:24:54.480: I/MainActivity(7647): proName : com.android.launcher3
  19. 03-27 14:24:54.480: I/MainActivity(7647): uid : 10053
  20. 03-27 14:24:54.480: I/MainActivity(7647): ================================
  21. 03-27 14:24:54.480: I/MainActivity(7647): importance: 400
  22. 03-27 14:24:54.480: I/MainActivity(7647): importanceReasonCode: 0
  23. 03-27 14:24:54.480: I/MainActivity(7647): impoartanceReasonComponent: null
  24. 03-27 14:24:54.480: I/MainActivity(7647): importanceReasonPid : 0
  25. 03-27 14:24:54.480: I/MainActivity(7647): lastTrimLevel: 0
  26. 03-27 14:24:54.480: I/MainActivity(7647): lru :0
  27. 03-27 14:24:54.490: I/MainActivity(7647): pid :770
  28. 03-27 14:24:54.490: I/MainActivity(7647): proName : com.android.settings
  29. 03-27 14:24:54.490: I/MainActivity(7647): uid : 1000
  30. 03-27 14:24:54.490: I/MainActivity(7647): ================================
  31. 03-27 14:24:54.490: I/MainActivity(7647): importance: 400
  32. 03-27 14:24:54.490: I/MainActivity(7647): importanceReasonCode: 0
  33. 03-27 14:24:54.490: I/MainActivity(7647): impoartanceReasonComponent: null
  34. 03-27 14:24:54.500: I/MainActivity(7647): importanceReasonPid : 0
  35. 03-27 14:24:54.500: I/MainActivity(7647): lastTrimLevel: 0
  36. 03-27 14:24:54.500: I/MainActivity(7647): lru :1
  37. 03-27 14:24:54.500: I/MainActivity(7647): pid :1092
  38. 03-27 14:24:54.500: I/MainActivity(7647): proName : com.android.mms
  39. 03-27 14:24:54.500: I/MainActivity(7647): uid : 10009
  40. 03-27 14:24:54.500: I/MainActivity(7647): ================================
  41. 03-27 14:24:54.500: I/MainActivity(7647): importance: 400
  42. 03-27 14:24:54.500: I/MainActivity(7647): importanceReasonCode: 0
  43. 03-27 14:24:54.500: I/MainActivity(7647): impoartanceReasonComponent: null
  44. 03-27 14:24:54.500: I/MainActivity(7647): importanceReasonPid : 0
  45. 03-27 14:24:54.500: I/MainActivity(7647): lastTrimLevel: 0
  46. 03-27 14:24:54.500: I/MainActivity(7647): lru :1
  47. 03-27 14:24:54.500: I/MainActivity(7647): pid :1353
  48. 03-27 14:24:54.500: I/MainActivity(7647): proName : com.baidu.appsearch
  49. 03-27 14:24:54.500: I/MainActivity(7647): uid : 10073
  50. 03-27 14:24:54.500: I/MainActivity(7647): ================================
  51. 03-27 14:24:54.510: I/MainActivity(7647): importance: 400
  52. 03-27 14:24:54.510: I/MainActivity(7647): importanceReasonCode: 0
  53. 03-27 14:24:54.510: I/MainActivity(7647): impoartanceReasonComponent: null
  54. 03-27 14:24:54.510: I/MainActivity(7647): importanceReasonPid : 0
  55. 03-27 14:24:54.510: I/MainActivity(7647): lastTrimLevel: 0
  56. 03-27 14:24:54.510: I/MainActivity(7647): lru :1
  57. 03-27 14:24:54.510: I/MainActivity(7647): pid :2131
  58. 03-27 14:24:54.510: I/MainActivity(7647): proName : com.android.defcontainer
  59. 03-27 14:24:54.510: I/MainActivity(7647): uid : 10004
  60. 03-27 14:24:54.510: I/MainActivity(7647): ================================
  61. 03-27 14:24:54.520: I/MainActivity(7647): importance: 300
  62. 03-27 14:24:54.520: I/MainActivity(7647): importanceReasonCode: 0
  63. 03-27 14:24:54.530: I/MainActivity(7647): impoartanceReasonComponent: null
  64. 03-27 14:24:54.530: I/MainActivity(7647): importanceReasonPid : 0
  65. 03-27 14:24:54.530: I/MainActivity(7647): lastTrimLevel: 0
  66. 03-27 14:24:54.530: I/MainActivity(7647): lru :0
  67. 03-27 14:24:54.530: I/MainActivity(7647): pid :979
  68. 03-27 14:24:54.530: I/MainActivity(7647): proName : com.infinit.wostore.ui
  69. 03-27 14:24:54.540: I/MainActivity(7647): uid : 10071
  70. 03-27 14:24:54.540: I/MainActivity(7647): ================================
  71. 03-27 14:24:54.540: I/MainActivity(7647): importance: 400
  72. 03-27 14:24:54.540: I/MainActivity(7647): importanceReasonCode: 0
  73. 03-27 14:24:54.550: I/MainActivity(7647): impoartanceReasonComponent: null
  74. 03-27 14:24:54.550: I/MainActivity(7647): importanceReasonPid : 0
  75. 03-27 14:24:54.550: I/MainActivity(7647): lastTrimLevel: 0
  76. 03-27 14:24:54.550: I/MainActivity(7647): lru :3
  77. 03-27 14:24:54.550: I/MainActivity(7647): pid :1712
  78. 03-27 14:24:54.550: I/MainActivity(7647): proName : com.android.quicksearchbox
  79. 03-27 14:24:54.550: I/MainActivity(7647): uid : 10054
  80. 03-27 14:24:54.550: I/MainActivity(7647): ================================

 

获取进程中的错误信息ProcessErrorStateInfo

 

  1. List<ProcessErrorStateInfo> list2 = activityManager.getProcessesInErrorState();
  2. if (list2 != null) {
  3. for (ProcessErrorStateInfo info : list2) {
  4. int condition = info.condition;// 进程进入的条件
  5. Log.i(TAG, "condition: " + condition);
  6. byte[] data = info.crashData;// crash数据
  7. if (data != null) {
  8. Log.i(TAG, "data: " + data.toString());
  9. } else {
  10. Log.i(TAG, "data: null");
  11. }
  12. String longMsg = info.longMsg;// 对条件condition的描述
  13. Log.i(TAG, "longMsg: " + longMsg);
  14. int pid = info.pid;// 进程id
  15. Log.i(TAG, "pid: " + pid);
  16. String proName = info.processName;// 进程名
  17. Log.i(TAG, "proName: " + proName);
  18. String shortMsg = info.shortMsg;
  19. Log.i(TAG, "shortMsg: " + shortMsg);
  20. String sTrace = info.stackTrace;// 堆栈追踪到的信息
  21. Log.i(TAG, "sTrace: " + sTrace);
  22. String tag = info.tag;// activity名是否与错误有关联
  23. Log.i(TAG, "tag: " + tag);
  24. int uid = info.uid;
  25. Log.i(TAG, "uid: " + uid);
  26. int describeContents = info.describeContents();// 数据包裹的描述
  27. Log.i(TAG, "describeContents: " + describeContents);
  28. }
  29. }


 

可惜,获得信息为空,也就无法打印信息查看。

 

获取任务信息RunningTaskInfo

 

  1. List<RunningTaskInfo> list3 = activityManager.getRunningTasks(10);
  2. if (list3 != null) {
  3. for (RunningTaskInfo info : list3) {
  4. ComponentName baseActivity = info.baseActivity;//任务主activity名
  5. Log.i(TAG, "baseActivity: " + baseActivity);
  6. CharSequence description = info.description;//任务的描述
  7. if(description!=null){
  8. Log.i(TAG, "description: " + description.toString());
  9. }else{
  10. Log.i(TAG, "description: null" );
  11. }
  12. int id = info.id;//任务的id号
  13. Log.i(TAG, "id: " + id);
  14. int numActivity = info.numActivities;//该任务的activity的数量
  15. Log.i(TAG, "numActivity: " + numActivity);
  16. int numRunning = info.numRunning;//当前活动的activity数量
  17. Log.i(TAG, "numRunning: " + numRunning);
  18. Bitmap bitmap = info.thumbnail;//缩略图
  19. Log.i(TAG, "bitmap: " + bitmap);
  20. ComponentName topActivity = info.topActivity;//当前活动activity中处于最顶端的activity
  21. Log.i(TAG, "topActivity: " + topActivity);
  22. int content = info.describeContents();//描述文本
  23. Log.i(TAG, "content: " + content);
  24. Log.i(TAG, "================================");
  25. }
  26. }


获取任务的方法需要添加权限:

 

<uses-permission android:name="android.permission.GET_TASKS"/>


运行程序输出信息如下:

 

  1. 03-27 16:05:18.990: I/MainActivity(10997): baseActivity: ComponentInfo{com.ibm.activitymanager/com.ibm.activitymanager.MainActivity}
  2. 03-27 16:05:18.990: I/MainActivity(10997): description: null
  3. 03-27 16:05:18.990: I/MainActivity(10997): id: 43
  4. 03-27 16:05:18.990: I/MainActivity(10997): numActivity: 1
  5. 03-27 16:05:18.990: I/MainActivity(10997): numRunning: 1
  6. 03-27 16:05:18.990: I/MainActivity(10997): bitmap: null
  7. 03-27 16:05:18.990: I/MainActivity(10997): topActivity: ComponentInfo{com.ibm.activitymanager/com.ibm.activitymanager.MainActivity}
  8. 03-27 16:05:18.990: I/MainActivity(10997): content: 0
  9. 03-27 16:05:18.990: I/MainActivity(10997): ================================
  10. 03-27 16:05:18.990: I/MainActivity(10997): baseActivity: ComponentInfo{com.android.launcher3/com.android.launcher3.Launcher}
  11. 03-27 16:05:18.990: I/MainActivity(10997): description: null
  12. 03-27 16:05:18.990: I/MainActivity(10997): id: 1
  13. 03-27 16:05:18.990: I/MainActivity(10997): numActivity: 1
  14. 03-27 16:05:18.990: I/MainActivity(10997): numRunning: 1
  15. 03-27 16:05:18.990: I/MainActivity(10997): bitmap: null
  16. 03-27 16:05:19.000: I/MainActivity(10997): topActivity: ComponentInfo{com.android.launcher3/com.android.launcher3.Launcher}
  17. 03-27 16:05:19.000: I/MainActivity(10997): content: 0
  18. 03-27 16:05:19.000: I/MainActivity(10997): ================================
  19. 03-27 16:05:19.000: I/MainActivity(10997): baseActivity: ComponentInfo{com.sohu.newsclient/com.sohu.newsclient.app.SplashActivity}
  20. 03-27 16:05:19.000: I/MainActivity(10997): description: null
  21. 03-27 16:05:19.000: I/MainActivity(10997): id: 39
  22. 03-27 16:05:19.000: I/MainActivity(10997): numActivity: 2
  23. 03-27 16:05:19.000: I/MainActivity(10997): numRunning: 2
  24. 03-27 16:05:19.000: I/MainActivity(10997): bitmap: null
  25. 03-27 16:05:19.000: I/MainActivity(10997): topActivity: ComponentInfo{com.sohu.newsclient/com.sohu.newsclient.app.NewsTabActivity}
  26. 03-27 16:05:19.000: I/MainActivity(10997): content: 0
  27. 03-27 16:05:19.000: I/MainActivity(10997): ================================
  28. 03-27 16:05:19.000: I/MainActivity(10997): baseActivity: ComponentInfo{com.android.settings/com.android.settings.Settings}
  29. 03-27 16:05:19.000: I/MainActivity(10997): description: null
  30. 03-27 16:05:19.000: I/MainActivity(10997): id: 14
  31. 03-27 16:05:19.000: I/MainActivity(10997): numActivity: 1
  32. 03-27 16:05:19.000: I/MainActivity(10997): numRunning: 1
  33. 03-27 16:05:19.000: I/MainActivity(10997): bitmap: null
  34. 03-27 16:05:19.000: I/MainActivity(10997): topActivity: ComponentInfo{com.android.settings/com.android.settings.Settings}
  35. 03-27 16:05:19.000: I/MainActivity(10997): content: 0
  36. 03-27 16:05:19.000: I/MainActivity(10997): ================================


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/472012
推荐阅读
相关标签
  

闽ICP备14008679号