29

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.

HippoMan
  • 2,119
  • 2
  • 25
  • 48

1 Answers1

63

You're looking for this, I think:

int id = android.os.Process.myPid();
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Yep, that's it! Thank you. I missed this because I was looking within java.lang.Process and not android.os.process. – HippoMan Sep 21 '14 at 20:49