如何从我在后台运行的服务知道最热门运行应用程序的包名称(android)

时间:2014-06-25 07:55:40

标签: android android-activity service android-package-managers

我如何知道顶级正在运行的应用程序的包名称。

https://www.dropbox.com/s/2p4iuj7dj7zv5u4/Screenshot%20%28234%29.png

(在链接中,上面的应用程序是运行2,包是com.imangi.templerun2)

我需要从我的服务中检索包名称(即com.imangi.templerun2)并将其存储在名为packageName的String中。 我该怎么办?

1 个答案:

答案 0 :(得分:0)

 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
 // Return a list of the tasks that are currently running,
 // with the most recent being first and older ones after in order.
 // Taken 1 inside getRunningTasks method means want to take only
 // top activity from stack and forgot the olders.
 List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

 ComponentName componentInfo = taskInfo.get(0).topActivity;
 componentInfo.getPackageName(); // here it is