与系统中所有的Activity进行交互的类。官网一句话解释了这个东西,但是有点随意了。ActivityManager的作用很多,我们通过它获得内存信息,进程信息,还可以终止某个进程。当然啦,只能终止用户的进程,系统的进程是杀死不了的。下面我通过实例,一步一步学习它的具体作用,我也是刚学,不能从宏观的角度给大家解释清楚它的具体作用。希望通过程序一步一步的写下去,其每个方法的作用也就清楚了,从而我们就知道它能干什么啦。语言组织能力差,所以就用代码来解释吧。
首先我们创建一个程序,在入口Activity里的onCreate()方法里,我们来创建ActivityManager.
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
从系统服务里得到我们的ActivityManger对象.在官方文档中,并没有提供构造方法,所以我们不能通过new的方式创建该对象,由于ActivityManager是核心服务,所以android从系统服务里得到它的实例。
MemoryInfo memInfo = new MemoryInfo();//存放内存信息的对象 activityManager.getMemoryInfo(memInfo);//传入参数,将获得数据保存在memInfo对象中 long availMem = memInfo.availMem/1000000;//可用内存 boolean isLowMem = memInfo.lowMemory;//是否达到最低内存 long threshold = memInfo.threshold/1000000;//临界值,达到这个值,进程就要被杀死 long totalMem = memInfo.totalMem/1000000;//总内存 Log.i(TAG, "avail:" + availMem + ",isLowMem:" + isLowMem + ",threshold:" + threshold + ",totalMem:" + totalMem);
在ActivityManager的getMemoryInfo的方法中需要传入一个MemoryInfo类,该类为ActivityManager内部类,手机的内存信息就保存在这个类中。然后通过MemoryInfo类中的方法,获得我们想要的信息。运行程序输出结果如下:
然后我们在调用一下清楚用户数据的方法:
activityManager.clearApplicationUserData()//清楚当前应用的用户数据
放在刚才获取内存的数据前面,我们看看清除当前应用的用户数据以后,我们的内存大小:
可以看出我们清除了1M的用户数据。
List<RunningAppProcessInfo> list1 = activityManager.getRunningAppProcesses(); for(RunningAppProcessInfo info :list1){ int importance = info.importance;//通过给进程标值来给进程分类,越重要的进程,那么数值就越低 Log.i(TAG, "importance: "+importance); int importanceReasonCode = info.importanceReasonCode;//重要性的原因,也是通过数值去判断。 Log.i(TAG, "importanceReasonCode: "+importanceReasonCode); ComponentName impoartanceReasonComponent = info.importanceReasonComponent;//重要性原因的组件,返回一个ComponentName的对象。 Log.i(TAG, "impoartanceReasonComponent: "+impoartanceReasonComponent); int importanceReasonPid = info.importanceReasonPid;//其他客户端进程的进程id号,如果没有其他进程返回0 Log.i(TAG, "importanceReasonPid : "+importanceReasonPid); int lastTrimLevel = info.lastTrimLevel;//提供给ComponentCallbacks2.onTrimMemory(int)方法的参数。上一次提交给进程的内饰水平 Log.i(TAG, "lastTrimLevel: "+lastTrimLevel); int lru = info.lru;//在一个特殊的进程中,提供更精细密度的衡量值,目前只维护IMPORTANCE_BACKGROUND Log.i(TAG, "lru :"+lru); int pid = info.pid;//进程id Log.i(TAG, "pid :"+pid); String proName = info.processName;//进程名 Log.i(TAG, "proName : " + proName); int uid = info.uid;//用户id Log.i(TAG, "uid : " + uid); Log.i(TAG, "================================"); }
运行程序得到输出,截取一部分如下:
03-27 14:24:54.460: I/MainActivity(7647): importance: 100 03-27 14:24:54.460: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.460: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.460: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.460: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.460: I/MainActivity(7647): lru :0 03-27 14:24:54.460: I/MainActivity(7647): pid :7647 03-27 14:24:54.460: I/MainActivity(7647): proName : com.ibm.activitymanager 03-27 14:24:54.470: I/MainActivity(7647): uid : 10072 03-27 14:24:54.470: I/MainActivity(7647): ================================ 03-27 14:24:54.470: I/MainActivity(7647): importance: 130 03-27 14:24:54.470: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.470: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.470: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.480: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.480: I/MainActivity(7647): lru :0 03-27 14:24:54.480: I/MainActivity(7647): pid :836 03-27 14:24:54.480: I/MainActivity(7647): proName : com.android.launcher3 03-27 14:24:54.480: I/MainActivity(7647): uid : 10053 03-27 14:24:54.480: I/MainActivity(7647): ================================ 03-27 14:24:54.480: I/MainActivity(7647): importance: 400 03-27 14:24:54.480: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.480: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.480: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.480: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.480: I/MainActivity(7647): lru :0 03-27 14:24:54.490: I/MainActivity(7647): pid :770 03-27 14:24:54.490: I/MainActivity(7647): proName : com.android.settings 03-27 14:24:54.490: I/MainActivity(7647): uid : 1000 03-27 14:24:54.490: I/MainActivity(7647): ================================ 03-27 14:24:54.490: I/MainActivity(7647): importance: 400 03-27 14:24:54.490: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.490: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.500: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.500: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.500: I/MainActivity(7647): lru :1 03-27 14:24:54.500: I/MainActivity(7647): pid :1092 03-27 14:24:54.500: I/MainActivity(7647): proName : com.android.mms 03-27 14:24:54.500: I/MainActivity(7647): uid : 10009 03-27 14:24:54.500: I/MainActivity(7647): ================================ 03-27 14:24:54.500: I/MainActivity(7647): importance: 400 03-27 14:24:54.500: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.500: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.500: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.500: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.500: I/MainActivity(7647): lru :1 03-27 14:24:54.500: I/MainActivity(7647): pid :1353 03-27 14:24:54.500: I/MainActivity(7647): proName : com.baidu.appsearch 03-27 14:24:54.500: I/MainActivity(7647): uid : 10073 03-27 14:24:54.500: I/MainActivity(7647): ================================ 03-27 14:24:54.510: I/MainActivity(7647): importance: 400 03-27 14:24:54.510: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.510: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.510: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.510: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.510: I/MainActivity(7647): lru :1 03-27 14:24:54.510: I/MainActivity(7647): pid :2131 03-27 14:24:54.510: I/MainActivity(7647): proName : com.android.defcontainer 03-27 14:24:54.510: I/MainActivity(7647): uid : 10004 03-27 14:24:54.510: I/MainActivity(7647): ================================ 03-27 14:24:54.520: I/MainActivity(7647): importance: 300 03-27 14:24:54.520: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.530: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.530: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.530: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.530: I/MainActivity(7647): lru :0 03-27 14:24:54.530: I/MainActivity(7647): pid :979 03-27 14:24:54.530: I/MainActivity(7647): proName : com.infinit.wostore.ui 03-27 14:24:54.540: I/MainActivity(7647): uid : 10071 03-27 14:24:54.540: I/MainActivity(7647): ================================ 03-27 14:24:54.540: I/MainActivity(7647): importance: 400 03-27 14:24:54.540: I/MainActivity(7647): importanceReasonCode: 0 03-27 14:24:54.550: I/MainActivity(7647): impoartanceReasonComponent: null 03-27 14:24:54.550: I/MainActivity(7647): importanceReasonPid : 0 03-27 14:24:54.550: I/MainActivity(7647): lastTrimLevel: 0 03-27 14:24:54.550: I/MainActivity(7647): lru :3 03-27 14:24:54.550: I/MainActivity(7647): pid :1712 03-27 14:24:54.550: I/MainActivity(7647): proName : com.android.quicksearchbox 03-27 14:24:54.550: I/MainActivity(7647): uid : 10054 03-27 14:24:54.550: I/MainActivity(7647): ================================
List<ProcessErrorStateInfo> list2 = activityManager.getProcessesInErrorState(); if (list2 != null) { for (ProcessErrorStateInfo info : list2) { int condition = info.condition;// 进程进入的条件 Log.i(TAG, "condition: " + condition); byte[] data = info.crashData;// crash数据 if (data != null) { Log.i(TAG, "data: " + data.toString()); } else { Log.i(TAG, "data: null"); } String longMsg = info.longMsg;// 对条件condition的描述 Log.i(TAG, "longMsg: " + longMsg); int pid = info.pid;// 进程id Log.i(TAG, "pid: " + pid); String proName = info.processName;// 进程名 Log.i(TAG, "proName: " + proName); String shortMsg = info.shortMsg; Log.i(TAG, "shortMsg: " + shortMsg); String sTrace = info.stackTrace;// 堆栈追踪到的信息 Log.i(TAG, "sTrace: " + sTrace); String tag = info.tag;// activity名是否与错误有关联 Log.i(TAG, "tag: " + tag); int uid = info.uid; Log.i(TAG, "uid: " + uid); int describeContents = info.describeContents();// 数据包裹的描述 Log.i(TAG, "describeContents: " + describeContents); } }
可惜,获得信息为空,也就无法打印信息查看。
List<RunningTaskInfo> list3 = activityManager.getRunningTasks(10); if (list3 != null) { for (RunningTaskInfo info : list3) { ComponentName baseActivity = info.baseActivity;//任务主activity名 Log.i(TAG, "baseActivity: " + baseActivity); CharSequence description = info.description;//任务的描述 if(description!=null){ Log.i(TAG, "description: " + description.toString()); }else{ Log.i(TAG, "description: null" ); } int id = info.id;//任务的id号 Log.i(TAG, "id: " + id); int numActivity = info.numActivities;//该任务的activity的数量 Log.i(TAG, "numActivity: " + numActivity); int numRunning = info.numRunning;//当前活动的activity数量 Log.i(TAG, "numRunning: " + numRunning); Bitmap bitmap = info.thumbnail;//缩略图 Log.i(TAG, "bitmap: " + bitmap); ComponentName topActivity = info.topActivity;//当前活动activity中处于最顶端的activity Log.i(TAG, "topActivity: " + topActivity); int content = info.describeContents();//描述文本 Log.i(TAG, "content: " + content); Log.i(TAG, "================================"); } }
获取任务的方法需要添加权限:
<uses-permission android:name="android.permission.GET_TASKS"/>
运行程序输出信息如下:
03-27 16:05:18.990: I/MainActivity(10997): baseActivity: ComponentInfo{com.ibm.activitymanager/com.ibm.activitymanager.MainActivity} 03-27 16:05:18.990: I/MainActivity(10997): description: null 03-27 16:05:18.990: I/MainActivity(10997): id: 43 03-27 16:05:18.990: I/MainActivity(10997): numActivity: 1 03-27 16:05:18.990: I/MainActivity(10997): numRunning: 1 03-27 16:05:18.990: I/MainActivity(10997): bitmap: null 03-27 16:05:18.990: I/MainActivity(10997): topActivity: ComponentInfo{com.ibm.activitymanager/com.ibm.activitymanager.MainActivity} 03-27 16:05:18.990: I/MainActivity(10997): content: 0 03-27 16:05:18.990: I/MainActivity(10997): ================================ 03-27 16:05:18.990: I/MainActivity(10997): baseActivity: ComponentInfo{com.android.launcher3/com.android.launcher3.Launcher} 03-27 16:05:18.990: I/MainActivity(10997): description: null 03-27 16:05:18.990: I/MainActivity(10997): id: 1 03-27 16:05:18.990: I/MainActivity(10997): numActivity: 1 03-27 16:05:18.990: I/MainActivity(10997): numRunning: 1 03-27 16:05:18.990: I/MainActivity(10997): bitmap: null 03-27 16:05:19.000: I/MainActivity(10997): topActivity: ComponentInfo{com.android.launcher3/com.android.launcher3.Launcher} 03-27 16:05:19.000: I/MainActivity(10997): content: 0 03-27 16:05:19.000: I/MainActivity(10997): ================================ 03-27 16:05:19.000: I/MainActivity(10997): baseActivity: ComponentInfo{com.sohu.newsclient/com.sohu.newsclient.app.SplashActivity} 03-27 16:05:19.000: I/MainActivity(10997): description: null 03-27 16:05:19.000: I/MainActivity(10997): id: 39 03-27 16:05:19.000: I/MainActivity(10997): numActivity: 2 03-27 16:05:19.000: I/MainActivity(10997): numRunning: 2 03-27 16:05:19.000: I/MainActivity(10997): bitmap: null 03-27 16:05:19.000: I/MainActivity(10997): topActivity: ComponentInfo{com.sohu.newsclient/com.sohu.newsclient.app.NewsTabActivity} 03-27 16:05:19.000: I/MainActivity(10997): content: 0 03-27 16:05:19.000: I/MainActivity(10997): ================================ 03-27 16:05:19.000: I/MainActivity(10997): baseActivity: ComponentInfo{com.android.settings/com.android.settings.Settings} 03-27 16:05:19.000: I/MainActivity(10997): description: null 03-27 16:05:19.000: I/MainActivity(10997): id: 14 03-27 16:05:19.000: I/MainActivity(10997): numActivity: 1 03-27 16:05:19.000: I/MainActivity(10997): numRunning: 1 03-27 16:05:19.000: I/MainActivity(10997): bitmap: null 03-27 16:05:19.000: I/MainActivity(10997): topActivity: ComponentInfo{com.android.settings/com.android.settings.Settings} 03-27 16:05:19.000: I/MainActivity(10997): content: 0 03-27 16:05:19.000: I/MainActivity(10997): ================================