Get Recent and Running application list not processes

后端 未结 3 1652
南方客
南方客 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:53

    For api level < 21

    You need this permission:

    
    

    And this code:

    ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
    List 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();
        }
    }
    

提交回复
热议问题