18

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
pushpa
  • 573
  • 2
  • 7
  • 19
  • 1
    Possible duplicate: http://stackoverflow.com/questions/3304685/how-to-get-the-list-of-running-applications – Rob Nov 11 '11 at 08:23
  • plz check this code http://stackoverflow.com/questions/3278895/how-to-check-current-running-applications-in-android thanks – Hemant Menaria Nov 11 '11 at 08:32
  • This doesn't appear to be an *exact* duplicate of the referenced questions; this one seems to be wanting the information when the apps are started and is also concerned only with apps running in the foreground. – Rob Hruska Nov 11 '11 at 13:00
  • @THelper : Thank you for the suggestion i was not aware of these things. – pushpa Nov 12 '11 at 04:53
  • The `ActivityManager :: getRunningTasks(int)` is deprecated since API 21. More information here. – Toochka Nov 07 '17 at 08:00

4 Answers4

24
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

Community
  • 1
  • 1
drooooooid
  • 1,574
  • 1
  • 11
  • 17
  • 17
    The method [`getRunningTasks(int maxNum)`, was deprecated in the API level 21(LOLLIPOP)](http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int)), for security reasons, the possibility of leaking personal information to third-party applications. (If you use this method to be aware that it will not be supported for future Android versions). – Fernando Leal Jan 12 '15 at 15:41
  • 1
    Anyone found a workaround that really works for doing the same thing as getRunningTasks(int maxNum) ? Thanks a lot! – Leo Melo Oct 22 '15 at 19:18
  • where I must put the code above? I try to put the code on onCreate and it just show the MainActivity even I try to open other app. It also just show MainActivity even I put the code on run() method of a service class – dev-x May 22 '18 at 12:50
10

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);
}
Caner
  • 57,267
  • 35
  • 174
  • 180
  • Thanks to all for your replay.From the above code i can get currently running applications info when i open my application.how can i do with out interrupting other application my app should get currently running info and it should display it in logcat. if some body knows please help i am struggling to do this. – pushpa Nov 11 '11 at 14:49
  • how can i find icon for currently running application ? – user3233280 Nov 23 '14 at 18:04
  • how could we know that specific task is can be terminated or not programatically? @Caner – gumuruh Apr 27 '22 at 09:22
2

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);
    }
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
elamathy
  • 57
  • 1
  • 4
1

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

auspicious99
  • 3,902
  • 1
  • 44
  • 58