{"id":358,"date":"2023-02-25T08:12:18","date_gmt":"2023-02-25T00:12:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=358"},"modified":"2023-04-30T14:44:34","modified_gmt":"2023-04-30T06:44:34","slug":"android-obtain-name-of-current-process-in-app","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-obtain-name-of-current-process-in-app\/","title":{"rendered":"Android\u83b7\u53d6APP\u5f53\u524d\u8fdb\u7a0b\u7684\u540d\u79f0"},"content":{"rendered":"<pre><code class=\"language-java\">public static String getProcessName(Context cxt, int pid) {\n    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);\n    List&lt;ActivityManager.RunningAppProcessInfo&gt; runningApps = am.getRunningAppProcesses();\n    if (runningApps == null) {\n        return null;\n    }\n    for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {\n        if (procInfo.pid == pid) {\n            return procInfo.processName;\n        }\n    }\n    return null;\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5728 Application \u7684 onCreate \u4e2d\u83b7\u53d6\u8fdb\u7a0b\u540d\u79f0\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5224\u65ad\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/cn.appblog.demo\nString processName = AppUtil.getProcessName(this, android.os.Process.myPid());\n\n\/\/\u5224\u65ad\u8fdb\u7a0b\u540d\uff0c\u4fdd\u8bc1\u53ea\u6709\u4e3b\u8fdb\u7a0b\u8fd0\u884c\nif (!TextUtils.isEmpty(processName) &amp;&amp; processName.equals(this.getPackageName())) {\n    \/\/\u4e3b\u8fdb\u7a0b\u521d\u59cb\u5316\u903b\u8f91\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public static String getProcessName(Context cxt, int pi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-358","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=358"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}