java Android:确定我的应用程序的进程 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25963677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:03:16  来源:igfitidea点击:

Android: determinine my app's process ID?

javaandroidpid

提问by HippoMan

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

在 Android 下运行的 Java 应用程序中有没有办法确定该应用程序的进程 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.

我想要这个的原因是我正在编写一个从 logcat 读取和处理行的应用程序,并且我希望能够忽略由这个应用程序本身编写的所有 logcat 行。

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.

我知道我可以通过在所有 Log.*() 调用中为我的应用程序使用唯一标记然后忽略带有此标记的 logcat 行来做到这一点,但在我看来,如果我可以简单地忽略所有 logcat 行会更清晰这是由我自己的应用程序的 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.

我已经搜索过,但找不到任何解释如何从该应用程序本身确定正在运行的 Android 应用程序的 PID 的内容。

Thanks in advance for any suggestions or pointers to docs.

在此先感谢您对文档的任何建议或指示。

回答by Ted Hopp

You're looking for this, I think:

你正在寻找这个,我想:

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