How to find the currently running applications programmatically in Android?

The question:

I am very new to Android. I am working on application that must get the information about the applications currently running on foreground.

It means if user launches any applications, my application should capture the launched application information, by the time my application shouldn’t interrupt launched application.

Example: If user launches browser application my application should print browser application information on log.

How can I do this?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

ActivityManager activity_manager = (ActivityManager) context

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()+ "tt ID: "+recentTasks.get(i).id+"");         

Also, have a look at following thread:
See Android recent task executed by the user

Method 2

You can use something similar to this:

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

for (int i = 0; i < tasks.size(); i++) {
    Log.d("Running task", "Running task: " + tasks.get(i).baseActivity.toShortString() + "tt ID: " + tasks.get(i).id);

Method 3

This code will provide the current acitvity logcat

Process logcatProc;
List <String> progs = new ArrayList <String>();

if (mBuffer != Buffer.MAIN) {
progs.add("*:" + mLevel);

logcatProc = Runtime.getRuntime()
                .exec(progs.toArray(new String[0]));

mReader = new BufferedReader(new InputStreamReader(
                logcatProc.getInputStream()), 1024);

String line;
while (mRunning && (line = mReader.readLine()) != null) {
    if (!mRunning) {
    if (line.length() == 0) {
    if (mIsFilterPattern) {
        if (mFilterPattern != null && !mFilterPattern.matcher(line).find()) {
    } else {
        if (mFilter != null && !line.toLowerCase().contains(mFilter.toLowerCase())) {
    synchronized (mLogCache) {

Method 4

ActivityManager.getRunningTasks(int) has been deprecated since API 21, for security reasons. You can use ActivityManager.RunningTaskInfo() to at least get the task info for the current task that this activity is running in (though some fields of RunningTaskInfo(), but not the whole method, are deprecated from API 29).

See Google’s docs for more info on RunningTaskInfo().

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment