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?

5
  • 1
    Possible duplicate: stackoverflow.com/questions/3304685/… Commented Nov 11, 2011 at 8:23
  • plz check this code stackoverflow.com/questions/3278895/… thanks Commented Nov 11, 2011 at 8: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. Commented Nov 11, 2011 at 13:00
  • @THelper : Thank you for the suggestion i was not aware of these things. Commented Nov 12, 2011 at 4:53
  • The ActivityManager :: getRunningTasks(int) is deprecated since API 21. More information <a href="developer.android.com/reference/android/app/…>. Commented Nov 7, 2017 at 8:00

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

The method getRunningTasks(int maxNum), was deprecated in the API level 21(LOLLIPOP), 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).
Anyone found a workaround that really works for doing the same thing as getRunningTasks(int maxNum) ? Thanks a lot!
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
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);
}

3 Comments

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.
how can i find icon for currently running application ?
how could we know that specific task is can be terminated or not programatically? @Caner
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);
    }
}

Comments

2

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

1 Comment

if you have the comparison code perhaps you may posted here.... @auspicious99

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.