Get Recent and Running application list not processes

后端 未结 3 1642
南方客
南方客 2020-12-06 13:05

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 b

相关标签:
3条回答
  • 2020-12-06 13:29

    Recent you can get from getRecentTasks which returns RecentTaskInfo.

    0 讨论(0)
  • 2020-12-06 13:51

    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.

    0 讨论(0)
  • 2020-12-06 13:53

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题