7

I tried a lot to get the list of recent and running applications(not processes) but couldn't get it.

I went through all the stackoverflow questions regarding this but the only answer i got about the running processes and recent tasks not the application specific like we can see in Samsung Mobile task manager on long press of home button.

I am able to get the Running processes by this code :

ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

for(RunningAppProcessInfo runningProInfo:procInfos){

        Log.d("Running Processes", "()()"+runningProInfo.processName);
}

It is giving the package names of all processes but i want only the package names of applications that i have launched(Both Recent and Running).

How can I extract that from Running Application Processes?

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111

3 Answers3

3

UPDATE: I don't see a way to get the name of the recent tasks even by using the below solution but this will work for the currently running and frozen tasks: getRunningTasks

Using getRunningTasks will return a list of RunningTaskInfo that contain the base ComponentName which you can use to get the package name from by calling getPackageName().

ORIGINAL ANSWER:

You should look into getRecentTasks. Its function is described as

Return a list of the tasks that the user has recently launched, with the most recent being first and older ones after in order.

Note: this method is only intended for debugging and presenting task management user interfaces.

This should get you a list of all the apps that have recently run or are running.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
  • I have tried it already and it returns a RecentTaskInfo objects list but i am not able to get the application name and other info from the objects? – Piyush Agarwal Sep 11 '12 at 20:02
  • 1
    Updated answer. The running tasks aren't the same as the recent tasks but it's close since it includes tasks that the user left and thinks are closed but are actually still running or frozen in the background. – Michael Celey Sep 11 '12 at 20:25
  • what about the RecentTask there also a origactivity from which we can get the package name but i ma getting why its is giving always null ? – Piyush Agarwal Sep 11 '12 at 20:48
  • baseintent is giving me the intent info in which pakage is also included but when i am calling the function getPackageName is it returning null why? – Piyush Agarwal Sep 11 '12 at 21:04
  • Package name and baseIntent could possibly be null if the activity was frozen and the system cleaned up the resources it was using. I can't find any method for getting package data that is more reliable though. I'll look into it a bit more. – Michael Celey Sep 12 '12 at 13:26
1

Recent you can get from getRecentTasks which returns RecentTaskInfo.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I have tried it already and it returns a RecentTaskInfo objects list but i am not able to get the application name and other info from the objects? – Piyush Agarwal Sep 11 '12 at 20:03
0

For api level < 21

You need this permission:

<uses-permission android:name="android.permission.GET_TASKS" />

And this code:

ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> recentTasks = activityManager.getRecentTasks(10, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
for (ActivityManager.RecentTaskInfo recentTask : recentTasks) {
    try {
        String packageName = recentTask.baseIntent.getComponent().getPackageName();
        ApplicationInfo appInfo = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        String label = getPackageManager().getApplicationLabel(appInfo).toString();
        Log.i("APPS", label);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}
Artificioo
  • 704
  • 1
  • 9
  • 19