How to find the currently running applications programmatically in Android?
ActivityManager activity_manager = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
ActivityManager
has method getRunningTasks(int)
. ActivityManager
seems to be the solution you are searching for.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");
}
Also, have a look at following thread:
See Android recent task executed by the user
How can I check if an app running on Android?
Add the below Helper class:
public class Helper {
public static boolean isAppRunning(final Context context, final String packageName) {
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
if (procInfos != null)
{
for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
if (processInfo.processName.equals(packageName)) {
return true;
}
}
}
return false;
}
}
Now you can check from the below code if your desired App is running or not:
if (Helper.isAppRunning(YourActivity.this, "com.your.desired.app")) {
// App is running
} else {
// App is not running
}
Related Topics
Listview Is Blank While Using Getfilter Function
How to Change Color of the Back Arrow in the New Material Theme
Opening a Shell and Interacting with Its I/O in Java
Loading Resources Using Getclass().Getresource()
Generate a Random Double in a Range
When Are Static Variables Initialized
How to Add and Remove Elements of Enumeration at Runtime in Java
Android: How to Stretch an Image to the Screen Width While Maintaining Aspect Ratio
How to View My Realm File in the Realm Browser
How to Create the Directory Error
Java.Lang.Classnotfoundexception in Spite of Using Classpath Environment Variable
Comparing Two Integer Arrays in Java
How to Read System Environment Variable in Spring Applicationcontext
What's the Syntax to Import a Class in a Default Package in Java