简体   繁体   English

Android 中所有正在运行的进程列表

[英]List of All running processes in Android

How to get the list of Android system's All running process including System launched processes?如何获取Android系统的所有正在运行的进程列表,包括系统启动的进程?

I tried to get the list using below code:我尝试使用以下代码获取列表:

ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();

This gave me the List of processes such as com.android.phone , com.android.chrome , etc.这给了我进程列表,例如com.android.phonecom.android.chrome等。

But when I run a ps command in my adb shell , I could see whole other bunch of processes running.但是当我在adb shell运行ps命令时,我可以看到其他一堆进程正在运行。 I am attaching the screenshot of all those processes running in my system.我附上了在我的系统中运行的所有这些进程的屏幕截图。在我的设备中运行进程

As one can see, there are several Android System's processes are also running like /system/bin/vold and /system/bin/installed , etc.可以看到,有几个 Android 系统的进程也在运行,如/system/bin/vold/system/bin/installed等。

However, these are not reported by getRunningAppProcesses() API.但是,这些不是由getRunningAppProcesses() API 报告的。 In its docs, it says that this API:在它的文档中,它说这个 API:

Returns a list of application processes that are running on the device.返回在设备上运行的应用程序进程列表。

Does this mean it won't return "system process"?这是否意味着它不会返回“系统进程”? And if that is the case what option developer can have to iterate over "ALL" process running on Android?如果是这种情况,开发人员可以选择哪些选项来迭代在 Android 上运行的“所有”进程?

-- What else I tried: Tried with 2 more APIs from ActivityManager : -我还尝试过什么:尝试了ActivityManager 2 个 API:

  1. getRecentTasks(int maxNum) and it's variant. getRecentTasks(int maxNum)及其变体。

But Android docs warns about its use as below:但是 Android 文档警告它的使用如下:

This method was deprecated in API level 21.此方法在 API 级别 21 中已弃用。

As of LOLLIPOP, this method is no longer available to third party applications从 LOLLIPOP 开始,此方法不再适用于第三方应用程序

  1. getRunningServices(int maxNum)

But both of these could not give me names like /system/bin/debuggerd , etc.但是这两个都不能给我像/system/bin/debuggerd等的名字。

NOTE: I am running Android-4.2 Jellybean, on Non-Rooted device.注意:我在非 Rooted 设备上运行 Android-4.2 Jellybean。

By calling an API from ActivityManager , you're only getting the applications which registered with it - that is, UI activities - and not all the processes.通过从ActivityManager调用 API,您只会获得向其注册的应用程序 - 即 UI 活动 - 而不是所有进程。 Those you see with a non-reverse DNS name, but a path (eg /system/bin/* ) are native daemons, started by init , and left out of the ActivityManager .您看到的那些具有非反向 DNS 名称但路径(例如/system/bin/* )是原生守护进程,由init启动,并被排除在ActivityManager

One way around this is to get the list of processes directly from /proc (just like toolbox's ps does it).解决此问题的一种方法是直接从/proc获取进程列表(就像工具箱的ps那样)。 This requires iterating programmatically over the directories there, (ie /proc/[0-9]* ), and pruning out the kernel threads.这需要以编程方式遍历那里的目录(即/proc/[0-9]* ),并修剪内核线程。 Kernel threads are those with a PPID of 2, so they're easy.内核线程是那些PPID为 2 的线程,因此它们很容易。 Native daemons will have a PPID of 1. Applications will have a PPID of Zygote.本机守护程序的PPID为 1。应用程序的PPID为 Zygote。

Reference: NewAndroidBook.com参考: NewAndroidBook.com

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

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