在一个应用中,或一个Service 、Receiver中判断一个应用是否正在运行,以便进行一些相关的处理。
这个时候我们需要得到一个ActivityManager,这个Manager顾名思意就是管理Activity的,它有一个方法叫getRunningTasks,可以得到当前系统正在运行的Task的列表,代码如下:

1 ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
2     List<RunningTaskInfo> list = am.getRunningTasks(100);
3     for (RunningTaskInfo info : list) {
4         if (info.topActivity.getPackageName().equals(MY_PKG_NAME) && info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
5             isAppRunning = true;
6             //find it, break
7             break;
8         }
9     }

100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表系统后台有此进程在运行,具体要做如何判断就看自已的业务需求。这个类还有更多的方法可以取得系统运行的服务、内存使用情况等的方法,请各位自行查找。

有一点要注意,如果想正常运行此方法,请在你的 AndroidManifest.xml 中加入: <uses-permission android:name="android.permission.GET_TASKS" /> 否则可能会有exception抛出。

 

 

Android系统内部状态信息的相关api:

得到ActivityManager : ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE)

这个位查到底是什么信息:   ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); 

获取进程内存状态的信息: Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(processIds);   获取当前运行的service信息: List<RunningServiceInfo> runningServiceInfos = activityManager.getRunningServices(MaxValue);  

获取当前运行的任务信息: List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(MaxValue); 其中runningTaskInfos 的 topActivity就是当前Task的活跃Activity 在getRunningTasks()所返回的Task队列中系统会根据这些Task的活跃度有一个排序,越活跃越是靠前。第一个就是当前活动的Task

 1 **
 2 * 检测某ActivityUpdate是否在当前Task的栈顶
 3 */
 4 public boolean isTopActivy(String cmdName){
 5         ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
 6         List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
 7         String cmpNameTemp = null;
 8 
 9         if(null != runningTaskInfos){
10                 cmpNameTemp=(runningTaskInfos.get(0).topActivity).toString);
11                 Log.e("cmpname","cmpname:"+cmpName);
12         }
13 
14        if(null == cmpNameTemp)return false;
15        return cmpNameTemp.equals(cmdName);
16 }

 

 

 1 /**get the launcher status */  
 2     private  boolean isLauncherRunnig(Context context) {  
 3             boolean result = false ;  
 4             List<String> names = getAllTheLauncher();  
 5             ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;  
 6             List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ;  
 7             for (RunningAppProcessInfo running : appList) {  
 8                     if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {  
 9                             for (int i = 0; i < names.size(); i++) {  
10                                     if (names.get(i).equals(running.processName)) {  
11                                             result = true ;  
12                                             break;  
13                                     }  
14                             }  
15                     }  
16             }  
17             return result ;  
18     }  

 

 1 private List<String> getAllTheLauncher(){  
 2         List<String> names = null;  
 3         PackageManager pkgMgt = this.getPackageManager();  
 4         Intent it = new Intent(Intent.ACTION_MAIN);     
 5         it.addCategory(Intent.CATEGORY_HOME);   
 6         List<ResolveInfo> ra =pkgMgt.queryIntentActivities(it,0);   
 7         if(ra.size() != 0){  
 8             names = new ArrayList<String>();  
 9         }  
10         for(int i=0;i< ra.size();i++)      
11         {  
12         String packageName =  ra.get(i).activityInfo.packageName;  
13         names.add(packageName);  
14         }    
15         return names;  
16     }  

 

1 android 取当前显示的activity是什么
2 ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
3 ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
4 Log.d("", "pkg:"+cn.getPackageName());
5 Log.d("", "cls:"+cn.getClassName());
 1 android 如何判断程序是否在前台运行
 2 private boolean isTopActivity(){  
 3         List<RunningTaskInfo>  tasksInfo = activityManager.getRunningTasks(1);  
 4         if(tasksInfo.size() > 0){  
 5             //应用程序位于堆栈的顶层  
 6             if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){  
 7                 return true;  
 8             }  
 9         }  
10         return false;  
11     }