Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Determining Whether a Given Application Is Running

Author: Colin Wilcox
Published? true -- FormatLanguage: W

Problem:

How do i know if mine or some other app is running?

Solution:

The system activity manager maintains a list of all active tasks. This provides the name of all running tasks and can be interrogated for various system specific information

Discussion:

This method takes the name of an application and returns true if the ActivityManager thinks it is currently running.

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;

public boolean isAppRunning (String aApplicationPackageName)
{
	ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
	if (activityManager == null)
	{
		return false; // should report: can't get Activity Manager
	}
			
	List<RunningAppProcessInfo> procInfos =
             activityManager.getRunningAppProcesses();
	for(int idx = 0; idx < procInfos.size(); idx++)
	{
		if(procInfos.get(i).processName.equals(aApplicationPackageName)) 
		{
			return true;
		}
	}
		
	return false;
}
No records found.