user2515165
user2515165

Reputation: 35

Android, how to get parent process id

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

Answers (2)

J.R.
J.R.

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

Edit:

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

Upvotes: 1

greenfeel
greenfeel

Reputation: 46

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

Upvotes: 3

Related Questions