簡體   English   中英

如何從在后台運行的我的服務中知道運行狀況最好的應用程序的程序包名稱(android)

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

我如何知道運行中的頂級應用程序的軟件包名稱。

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

(在鏈接中,上面的應用程序是Temple run 2,包是com.imangi.templerun2)

我需要從服務中檢索包名稱(即com.imangi.templerun2),並將其存儲在名為packageName的字符串中。 我該怎么做 ???

 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