Android: determinine my app's process ID?

HippoMan picture HippoMan · Sep 21, 2014 · Viewed 20.1k times · Source

Is there a way within a java app that's running under Android to determine that app's process ID ("PID")?

The reason I want this is that I am writing an app which reads and processes lines from logcat, and I want to be able to ignore all logcat lines that have been written by this app, itself.

I know I can do this by using a unique tag for my app in all the Log.*() calls and then ignoring logcat lines with this tag, but it seems to me that it would be cleaner if I could simply ignore all logcat lines which have been written by my own app's PID.

I've searched, but I couldn't find anything which explains how to determine the PID of a running Android app, from within that app, itself.

Thanks in advance for any suggestions or pointers to docs.

Answer

Ted Hopp picture Ted Hopp · Sep 21, 2014

You're looking for this, I think:

int id = android.os.Process.myPid();