Android如何判断一个应用在运行

时间:2021-08-10 22:49:08

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

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

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

有一点要注意,如果想正常运行此方法,请在你的 AndroidManifest.xml 中加入:

<uses-permission android:name="android.permission.GET_TASKS" />  
/**
* 判断应用是否在运行
* @param context
* @return
*/
public boolean isRun(Context context){
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "com.ad";
//100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表系统后台有此进程在运行
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
Log.i("ActivityService isRun()",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
break;
}
} Log.i("ActivityService isRun()", "com.ad 程序 ...isAppRunning......"+isAppRunning);
return isAppRunning;
}

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

    

/**
* 检测某ActivityUpdate是否在当前Task的栈顶
*/
public boolean isTopActivy(String cmdName){
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
String cmpNameTemp = null; if(null != runningTaskInfos){
cmpNameTemp=(runningTaskInfos.get(0).topActivity).toString);
Log.e("cmpname","cmpname:"+cmpName);
} if(null == cmpNameTemp)return false;
return cmpNameTemp.equals(cmdName);
}
/**get the launcher status */
private boolean isLauncherRunnig(Context context) {
boolean result = false ;
List<String> names = getAllTheLauncher();
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;
List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ;
for (RunningAppProcessInfo running : appList) {
if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(running.processName)) {
result = true ;
break;
}
}
}
}
return result ;
}
/**
* 得到所有的Launcher
*/
private List<String> getAllTheLauncher(){
List<String> names = null;
PackageManager pkgMgt = this.getPackageManager();
Intent it = new Intent(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> ra =pkgMgt.queryIntentActivities(it,0);
if(ra.size() != 0){
names = new ArrayList<String>();
}
for(int i=0;i< ra.size();i++)
{
String packageName = ra.get(i).activityInfo.packageName;
names.add(packageName);
}
return names;
}

android 取当前显示的activity :

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
Log.d("", "pkg:"+cn.getPackageName());
Log.d("", "cls:"+cn.getClassName());

Android如何判断程序是否在前台运行:

private boolean isTopActivity(){
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if(tasksInfo.size() > 0){
//应用程序位于堆栈的顶层
if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){
return true;
}
}
return false;
}