本文介绍了Android 中所有正在运行的进程列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取Android系统的所有正在运行的进程列表,包括系统启动的进程?

我尝试使用以下代码获取列表:

ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );ListprocInfos = activityManager.getRunningAppProcesses();

这给了我com.android.phonecom.android.chrome等进程列表

但是当我在我的 adb shell 中运行一个 ps 命令时,我可以看到其他一堆正在运行的进程.我附上了在我的系统中运行的所有这些进程的屏幕截图.

可以看到,Android System 的几个进程也在运行,比如/system/bin/vold/system/bin/installed 等>

但是,getRunningAppProcesses() API 不会报告这些.在它的文档中,它说这个 API:

返回在设备上运行的应用进程列表.

这是否意味着它不会返回系统进程"?如果是这种情况,开发人员必须迭代所有"选项的选项是什么?在 Android 上运行的进程?

-- 我还尝试了什么:尝试使用 ActivityManager 中的另外 2 个 API:

  1. getRecentTasks(int maxNum) 和它的变体.

但 Android 文档警告其使用如下:

此方法已在 API 级别 21 中弃用.

从 LOLLIPOP 开始,此方法不再适用于第三方应用程序

  1. getRunningServices(int maxNum)

但是这两个都不能给我像 /system/bin/debuggerd 之类的名字

注意:我在非根设备上运行 Android-4.2 Jellybean.

解决方案

通过从 ActivityManager 调用 API,您只会获得注册到它的应用程序 - 即 UI 活动 - 并且不是所有流程.您看到的那些具有非反向 DNS 名称但路径(例如 /system/bin/*)是本机守护进程,由 init 启动,并被排除在ActivityManager.

解决此问题的一种方法是直接从 /proc 获取进程列表(就像工具箱的 ps 所做的那样).这需要以编程方式遍历那里的目录(即 /proc/[0-9]*),并修剪内核线程.内核线程是那些 PPID 为 2 的线程,因此它们很容易.本机守护程序的 PPID 为 1.应用程序的 PPID 为 Zygote.

参考:NewAndroidBook.com

How to get the list of Android system's All running process including System launched processes?

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.

But when I run a ps command in my adb shell, I could see whole other bunch of processes running. 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.

However, these are not reported by getRunningAppProcesses() API. In its docs, it says that this API:

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?

-- What else I tried:Tried with 2 more APIs from ActivityManager:

  1. getRecentTasks(int maxNum) and it's variant.

But Android docs warns about its use as below:

  1. getRunningServices(int maxNum)

But both of these could not give me names like /system/bin/debuggerd, etc.

NOTE: I am running Android-4.2 Jellybean, on Non-Rooted device.

解决方案

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. Those you see with a non-reverse DNS name, but a path (e.g. /system/bin/*) are native daemons, started by init, and left out of the ActivityManager.

One way around this is to get the list of processes directly from /proc (just like toolbox's ps does it). This requires iterating programmatically over the directories there, (i.e. /proc/[0-9]*), and pruning out the kernel threads. Kernel threads are those with a PPID of 2, so they're easy. Native daemons will have a PPID of 1. Applications will have a PPID of Zygote.

Reference: NewAndroidBook.com

这篇关于Android 中所有正在运行的进程列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:15
查看更多