How to find the currently running applications programmatically in Android?

This code will provide the current acitvity logcat

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

progs.add("logcat");
progs.add("-v");
progs.add(mFormat.getValue());
if (mBuffer != Buffer.MAIN) {
        progs.add("-b");
        progs.add(mBuffer.getValue());
}
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) {
            break;
    }
    if (line.length() == 0) {
            continue;
    }
    if (mIsFilterPattern) {
        if (mFilterPattern != null && !mFilterPattern.matcher(line).find()) {
            continue;
        }
    } else {
        if (mFilter != null && !line.toLowerCase().contains(mFilter.toLowerCase())) {
            continue;
        }
    }
    synchronized (mLogCache) {
            mLogCache.add(line);
    }
}

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().


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() + "\t\t ID: " + tasks.get(i).id);
}

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