-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessUtils.kt
More file actions
43 lines (41 loc) · 1.22 KB
/
ProcessUtils.kt
File metadata and controls
43 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.handy.basic.utils
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
/**
* @title: ProcessUtils
* @projectName HandyBasicKT
* @description: 进程相关工具类
* @author LiuJie https://github.com/Handy045
* @date Created in 2019-10-28 16:34
*/
class ProcessUtils private constructor() {
companion object {
/**
* 获取进程号对应的进程名
*
* @param pid 进程号
* @return 进程名
*/
fun getProcessName(pid: Int): String? {
var reader: BufferedReader? = null
try {
reader = BufferedReader(FileReader("/proc/$pid/cmdline"))
var processName = reader.readLine()
if (!processName.isNullOrEmpty()) {
processName = processName.trim()
}
return processName
} catch (throwable: Throwable) {
throwable.printStackTrace()
} finally {
try {
reader?.close()
} catch (exception: IOException) {
exception.printStackTrace()
}
}
return null
}
}
}