Reputation: 35
On Android device , given a process id, I want to programatically find the parent process ID. I have searched a lot but unable to find the answer.
Upvotes: 1
Views: 1000
Reputation: 76
You can use Runtime
to execute a command on the command line and get the parent process id (PPID).
By executing the ps -o ppid= $pid
command, you can get the PPID of the process id passed in as $pid
.
Here's an example in Kotin:
val pid = android.os.Process.myPid()
val command = "ps -o ppid= $pid"
val p = Runtime.getRuntime().exec(command);
val bufferedReader = BufferedReader(InputStreamReader(p.getInputStream()))
val result = bufferedReader.readText().trim().toInt()
@GreenFeel's comment on their answer describes a simpler way using android.system.Os.getppid()
.
Upvotes: 1