我如何知道顶级正在运行的应用程序的包名称。
https://www.dropbox.com/s/2p4iuj7dj7zv5u4/Screenshot%20%28234%29.png
(在链接中,上面的应用程序是运行2,包是com.imangi.templerun2)
我需要从我的服务中检索包名称(即com.imangi.templerun2)并将其存储在名为packageName的String中。 我该怎么办?
答案 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