如何在Android中以编程方式查找当前正在运行的应用程序? [英] How to find the currently running applications programmatically in Android?
问题描述
我是Android新手。我正在开发应用程序,必须获取有关当前正在前台运行的应用程序的信息。
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.
我该怎么做?
推荐答案
ActivityManager activity_manager = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
ActivityManager
有方法 getRunningTasks(INT)
。 ActivityManager
似乎是您要搜索的解决方案。
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+"");
}
另外,看看以下帖子:
查看用户执行的Android最近任务
Also, have a look at following thread: See Android recent task executed by the user
这篇关于如何在Android中以编程方式查找当前正在运行的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!