如何在Android中检查当前正在运行的应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3278895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check current running applications in Android?
提问by UMAR
I want to check current running applications in android programmatically, similar to how it shows 6 applications if we press and hold the HOMEbutton.
我想以编程方式检查 android 中当前正在运行的应用程序,类似于如果我们按住HOME按钮它显示 6 个应用程序的方式。
I'm mostly interested in the application names.
我最感兴趣的是应用程序名称。
回答by Anoop
You can get current name package using
您可以使用获取当前名称包
ActivityManager am = (ActivityManager) mContext
.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity
.getPackageName();
You can use this package name to get current active application
您可以使用此包名称来获取当前活动的应用程序
回答by Nam Vu
You can check the processName of each element in the list to see if it's the process you're looking for. You can use this code
您可以检查列表中每个元素的 processName 以查看它是否是您要查找的进程。您可以使用此代码
boolean isNamedProcessRunning(String processName){
if (processName == null)
return false;
ActivityManager manager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes)
{
if (processName.equals(process.processName))
{
return true;
}
}
return false;
}
回答by Tim Kryger
A long press of the HOME key brings up a list of recent (not necessarily running) tasks.
长按 HOME 键会显示最近(不一定正在运行)任务的列表。
ActivityManager
has what you need. Look at getRunningAppProcesses
or getRunningTasks
.
ActivityManager
有你需要的东西。看看getRunningAppProcesses
或getRunningTasks
。
You can view this information (and much more) by running dumpsys activity
via 'adb shell'.
您可以dumpsys activity
通过“adb shell”运行来查看此信息(以及更多信息)。
Running processes (most recent first):
App #11: adj=fore /F 43fe7c20 233:com.android.browser/10004 (top-activity)
App #10: adj=bak /B 43dcec80 190:android.process.media/10009 (bg-empty)
App # 9: adj=vis /F 43f495c8 107:com.android.inputmethod.latin/10014 (service)
com.android.inputmethod.latin.LatinIME<=ProcessRecord{43dbe0e8 59:system/1000}
PERS # 8: adj=sys /F 43dbe0e8 59:system/1000 (fixed)
PERS # 7: adj=core /F 43f534c0 111:com.android.phone/1001 (fixed)
App # 6: adj=bak+1/B 43ea1f58 148:android.process.acore/10006 (bg-empty)
App # 5: adj=home /B 43f601c0 114:com.android.launcher/10000 (home)
App # 4: adj=bak+2/B 43f85128 133:com.android.settings/1000 (bg-empty)
App # 3: adj=bak+3/B 43eacae0 223:com.android.music/10029 (bg-empty)
App # 2: adj=bak+4/B 43dfc500 206:com.android.mms/10028 (bg-empty)
App # 1: adj=bak+5/B 43f8fcd0 166:com.android.alarmclock/10025 (bg-empty)
App # 0: adj=bak+6/B 43fcbe50 182:com.android.email/10008 (bg-empty)
PID mappings:
PID #59: ProcessRecord{43dbe0e8 59:system/1000}
PID #107: ProcessRecord{43f495c8 107:com.android.inputmethod.latin/10014}
PID #111: ProcessRecord{43f534c0 111:com.android.phone/1001}
PID #114: ProcessRecord{43f601c0 114:com.android.launcher/10000}
PID #133: ProcessRecord{43f85128 133:com.android.settings/1000}
PID #148: ProcessRecord{43ea1f58 148:android.process.acore/10006}
PID #166: ProcessRecord{43f8fcd0 166:com.android.alarmclock/10025}
PID #182: ProcessRecord{43fcbe50 182:com.android.email/10008}
PID #190: ProcessRecord{43dcec80 190:android.process.media/10009}
PID #206: ProcessRecord{43dfc500 206:com.android.mms/10028}
PID #223: ProcessRecord{43eacae0 223:com.android.music/10029}
PID #233: ProcessRecord{43fe7c20 233:com.android.browser/10004}