自 2026 年起,为了与我们的主干稳定开发模型保持一致,并确保生态系统的平台稳定性,我们将在第 2 季度和第 4 季度将源代码发布到 AOSP。对于构建 AOSP 和向 AOSP 贡献代码,我们建议使用 android-latest-release 而不是 aosp-main。android-latest-release 清单分支将始终引用推送到 AOSP 的最新版本。如需了解详情,请参阅 AOSP 变更。
Google uses AI technology to translate content into your preferred language. AI translations can contain errors.
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
ProcessUtil
public
final
class
ProcessUtil
extends Object
| java.lang.Object
|
| ↳ |
com.android.sts.common.ProcessUtil
|
用于在设备上查找、等待和终止进程的各种辅助程序
摘要
公共方法 |
static
Optional<IFileEntry>
|
findFileLoadedByProcess(ITestDevice device, String process, String filenameSubstr)
返回指定进程加载的第一个文件(具有指定名称)的文件条目
|
static
Optional<List<String>>
|
findFilesLoadedByProcess(ITestDevice device, int pid, Pattern filePattern)
返回由指定进程加载的指定文件的文件名。
|
static
Optional<String>
|
getProcessName(ITestDevice device, int pid)
从 /proc/pid/cmdline 获取内容。
|
static
boolean
|
killAll(ITestDevice device, String pgrepRegex, long timeoutMs, boolean expectExist)
向与模式匹配的所有进程发送 SIGKILL。
|
static
boolean
|
killAll(ITestDevice device, String pgrepRegex, long timeoutMs)
向与模式匹配的所有进程发送 SIGKILL。
|
static
void
|
killPid(ITestDevice device, int pid, long timeoutMs)
向进程发送 SIGKILL 信号,并等待其退出。
|
static
void
|
killPid(ITestDevice device, int pid, int signal, long timeoutMs)
向进程发送信号,并等待其退出。
|
static
Optional<List<String>>
|
listOpenFiles(ITestDevice device, int pid)
返回指定进程当前打开的文件名称。
|
static
Optional<Integer>
|
pidOf(ITestDevice device, String pgrepRegex)
获取与传递给 `pgrep` 的模式匹配的单个 pid。
|
static
Optional<Map<Integer, String>>
|
pidsOf(ITestDevice device, String pgrepRegex)
获取传递给 `pgrep` 的模式匹配的 pid。
|
static
void
|
waitPidExited(ITestDevice device, int pid)
等待进程退出。
|
static
void
|
waitPidExited(ITestDevice device, int pid, long timeoutMs)
等待进程退出。
|
static
Map<Integer, String>
|
waitProcessRunning(ITestDevice device, String pgrepRegex, long timeoutMs)
等待找到给定正则表达式的正在运行的进程。
|
static
Map<Integer, String>
|
waitProcessRunning(ITestDevice device, String pgrepRegex)
等待找到给定正则表达式的正在运行的进程。
|
static
AutoCloseable
|
withProcessKill(ITestDevice device, String pgrepRegex, Runnable beforeCloseKill, long timeoutMs)
在测试开始和结束时终止进程。
|
static
AutoCloseable
|
withProcessKill(ITestDevice device, String pgrepRegex, Runnable beforeCloseKill)
在测试开始和结束时终止进程。
|
常量
PROCESS_POLL_PERIOD_MS
public static final long PROCESS_POLL_PERIOD_MS
常量值:
250
(0x00000000000000fa)
PROCESS_WAIT_TIMEOUT_MS
public static final long PROCESS_WAIT_TIMEOUT_MS
常量值:
10000
(0x0000000000002710)
公共方法
findFileLoadedByProcess
public static Optional<IFileEntry> findFileLoadedByProcess (ITestDevice device,
String process,
String filenameSubstr)
返回指定进程加载的第一个文件(具有指定名称)的文件条目
| 参数 |
device |
ITestDevice:要运行的设备 |
process |
String:要查找的进程的 pgrep 模式 |
filenameSubstr |
String:进程加载的文件名/路径的一部分 |
| 返回 |
Optional<IFileEntry> |
设备上文件路径的 IFileEntry(如果有)的选项。 |
| 抛出 |
DeviceNotAvailableException |
|
findFilesLoadedByProcess
public static Optional<List<String>> findFilesLoadedByProcess (ITestDevice device,
int pid,
Pattern filePattern)
返回由指定进程加载的指定文件的文件名。
| 参数 |
device |
ITestDevice:要运行的设备 |
pid |
int:要搜索的进程的 ID |
filePattern |
Pattern:要返回的文件名的模式 |
| 返回 |
Optional<List<String>> |
过滤后的文件的选填项;如果找不到进程或无法读取打开的文件,则为空。
|
| 抛出 |
DeviceNotAvailableException |
|
getProcessName
public static Optional<String> getProcessName (ITestDevice device,
int pid)
从 /proc/pid/cmdline 获取内容。
| 参数 |
device |
ITestDevice:要使用的设备 |
pid |
int:要获取其名称的进程的 ID |
| 返回 |
Optional<String> |
/proc/pid/cmdline 内容的可选字符串;如果找不到 pid,则为空
|
| 抛出 |
DeviceNotAvailableException |
|
killAll
public static boolean killAll (ITestDevice device,
String pgrepRegex,
long timeoutMs,
boolean expectExist)
向与模式匹配的所有进程发送 SIGKILL。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:表示 pgrep 的正则表达式的字符串 |
timeoutMs |
long:在抛出 TimeoutException 之前等待多长时间 |
expectExist |
boolean:在没有终止任何进程时是否应抛出异常 |
killAll
public static boolean killAll (ITestDevice device,
String pgrepRegex,
long timeoutMs)
向与模式匹配的所有进程发送 SIGKILL。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:表示 pgrep 的正则表达式的字符串 |
timeoutMs |
long:在抛出 TimeoutException 之前等待多长时间 |
killPid
public static void killPid (ITestDevice device,
int pid,
long timeoutMs)
向进程发送 SIGKILL 信号,并等待其退出。
| 参数 |
device |
ITestDevice:要使用的设备 |
pid |
int:要等待退出的过程的 ID |
timeoutMs |
long:在抛出 TimeoutException 之前等待多长时间 |
killPid
public static void killPid (ITestDevice device,
int pid,
int signal,
long timeoutMs)
向进程发送信号,并等待其退出。
| 参数 |
device |
ITestDevice:要使用的设备 |
pid |
int:要等待退出的过程的 ID |
signal |
int:要发送给进程的信号 |
timeoutMs |
long:在抛出 TimeoutException 之前等待多长时间 |
listOpenFiles
public static Optional<List<String>> listOpenFiles (ITestDevice device,
int pid)
返回指定进程当前打开的文件名称。
| 参数 |
device |
ITestDevice:要运行的设备 |
pid |
int:要搜索的进程的 ID |
| 返回 |
Optional<List<String>> |
打开的文件的选填项;如果找不到进程或无法读取打开的文件,则为空。
|
| 抛出 |
DeviceNotAvailableException |
|
pidOf
public static Optional<Integer> pidOf (ITestDevice device,
String pgrepRegex)
获取与传递给 `pgrep` 的模式匹配的单个 PID。如果有多个 PID 与模式匹配,则抛出 IllegalArgumentException。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:表示 pgrep 的正则表达式的字符串 |
| 返回 |
Optional<Integer> |
pid 的可选整数;如果 pgrep 未返回 EXIT_SUCCESS,则为空
|
| 抛出 |
DeviceNotAvailableException |
|
IllegalArgumentException |
|
pidsOf
public static Optional<Map<Integer, String>> pidsOf (ITestDevice device,
String pgrepRegex)
获取传递给 `pgrep` 且与模式匹配的 pid。由于 /proc/pid/comm 被截断,因此 `pgrep` 会通过 `-f` 传递,以检查完整的命令行。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:表示 pgrep 的正则表达式的字符串 |
| 返回 |
Optional<Map<Integer, String>> |
可选的 pid 到命令行的映射;如果 pgrep 未返回 EXIT_SUCCESS,则为空
|
| 抛出 |
DeviceNotAvailableException |
|
waitPidExited
public static void waitPidExited (ITestDevice device,
int pid)
等待进程退出。这不是等待它发生变化,而是等待它不存在。在轮询之间重复使用 pid 是可能的,但不太可能
| 参数 |
device |
ITestDevice:要使用的设备 |
pid |
int:要等待退出的过程的 ID |
waitPidExited
public static void waitPidExited (ITestDevice device,
int pid,
long timeoutMs)
等待进程退出。这不是等待它发生变化,而是等待它不存在。在轮询之间重复使用 pid 是可能的,但不太可能
| 参数 |
device |
ITestDevice:要使用的设备 |
pid |
int:要等待退出的过程的 ID |
timeoutMs |
long:在抛出 TimeoutException 之前等待多长时间 |
waitProcessRunning
public static Map<Integer, String> waitProcessRunning (ITestDevice device,
String pgrepRegex,
long timeoutMs)
等待找到给定正则表达式的正在运行的进程。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:表示 pgrep 的正则表达式的字符串 |
timeoutMs |
long:在抛出 TimeoutException 之前等待多长时间 |
| 返回 |
Map<Integer, String> |
从 pidsOf(...) 获取的 pid 到命令的映射
|
| 抛出 |
TimeoutException |
|
DeviceNotAvailableException |
|
waitProcessRunning
public static Map<Integer, String> waitProcessRunning (ITestDevice device,
String pgrepRegex)
等待找到给定正则表达式的正在运行的进程。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:表示 pgrep 的正则表达式的字符串 |
| 返回 |
Map<Integer, String> |
从 pidsOf(...) 获取的 pid 到命令的映射
|
| 抛出 |
TimeoutException |
|
DeviceNotAvailableException |
|
withProcessKill
public static AutoCloseable withProcessKill (ITestDevice device,
String pgrepRegex,
Runnable beforeCloseKill,
long timeoutMs)
在测试开始和结束时终止进程。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:要提供给 pgrep 的要终止的进程的名称模式 |
beforeCloseKill |
Runnable:用于在测试结束时在正常环境中终止进程之前需要清理任何操作的可运行对象。可以为 null。 |
timeoutMs |
long:等待进程终止的时长(以毫秒为单位) |
| 返回 |
AutoCloseable |
一个对象,用于在进程关闭时再次终止进程 |
withProcessKill
public static AutoCloseable withProcessKill (ITestDevice device,
String pgrepRegex,
Runnable beforeCloseKill)
在测试开始和结束时终止进程。
| 参数 |
device |
ITestDevice:要使用的设备 |
pgrepRegex |
String:要提供给 pgrep 的要终止的进程的名称模式 |
beforeCloseKill |
Runnable:用于在测试结束时在正常环境中终止进程之前需要清理任何操作的可运行对象。可以为 null。 |
| 返回 |
AutoCloseable |
一个对象,用于在进程关闭时再次终止进程 |
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[]]