简体   繁体   English

获取最近并运行的应用程序列表不是进程

[英]Get Recent and Running application list not processes

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. 我查看了有关此问题的所有stackoverflow问题,但我得到的关于运行进程和最近任务的唯一答案不是特定应用程序,就像我们在长按主页按钮时可以在Samsung Mobile任务管理器中看到的那样。

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). 它给出了所有进程的包名,但我只想要我已经启动的应用程序的包名(Both Recent和Running)。

How can I extract that from Running Application Processes? 如何从运行应用程序进程中提取它?

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 更新:即使使用以下解决方案,我也看不到获取最近任务名称的方法,但这将适用于当前正在运行和冻结的任务: 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() . 使用getRunningTasks将返回一个RunningTaskInfo列表,其中包含基本ComponentName ,您可以通过调用getPackageName()来获取包名称。

ORIGINAL ANSWER: 原始答案:

You should look into getRecentTasks . 您应该查看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. 这应该会为您提供最近运行或正在运行的所有应用程序的列表。

最近你可以从getRecentTasks获得它返回RecentTaskInfo

For api level < 21 对于api级别<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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM