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?

Solution 1

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

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

Solution 3

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);
    }
}

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