Home > Software design >  Android, how to get parent process id
Android, how to get parent process id

Time:08-30

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.

CodePudding user response:

Maybe you can try int ppid = android.os.Process.myPpid()

CodePudding user response:

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()

Edit:

@GreenFeel's comment on their answer describes a simpler way using android.system.Os.getppid().

  • Related