简体   繁体   English

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

[英]How to know Package Name of a Top running app from my service running in background (android)

How can i know the package name of top Running application. 我如何知道运行中的顶级应用程序的软件包名称。

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

(in the link , above application is temple run 2 and package is com.imangi.templerun2 ) (在链接中,上面的应用程序是Temple run 2,包是com.imangi.templerun2)

I need to retrieve the package name(ie com.imangi.templerun2) from my service and store it in String named packageName . 我需要从服务中检索包名称(即com.imangi.templerun2),并将其存储在名为packageName的字符串中。 How can i do it ??? 我该怎么做 ???

 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

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

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