{"id":229993,"date":"2021-11-19T08:32:43","date_gmt":"2021-11-19T00:32:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=229993"},"modified":"2021-11-14T18:33:08","modified_gmt":"2021-11-14T10:33:08","slug":"multi-process-communication","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/multi-process-communication.html","title":{"rendered":"\u591a\u8fdb\u7a0b\u901a\u4fe1\u7cfb\u5217\u95ee\u9898"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nAndroid\u4e2d\u5f00\u542f\u591a\u8fdb\u7a0b\u53ea\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u5c31\u662f\u5728AndroidManifest.xml\u4e2d\u6ce8\u518cService\u3001Activity\u3001Receiver\u3001ContentProvider\u65f6\u6307\u5b9aandroid:process\u5c5e\u6027\u3002\u4eca\u5929\u6765\u8bb2\u89e3\u4e0b\uff1a\u591a\u8fdb\u7a0b\u901a\u4fe1\u65b9\u5f0f\u4ee5\u53ca\u5e26\u6765\u7684\u95ee\u9898\uff0c\u65b9\u4fbf\u5728\u9879\u76ee\u4e2d\u9047\u5230\u95ee\u9898\u53ca\u65f6\u7684\u5904\u7406;<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001Android\u4e2d\u591a\u8fdb\u7a0b\u8be6\u89e3<\/strong><\/div>\n
1\u3001\u5b9a\u4e49<\/strong><\/span><\/div>\n
    Android\u7684\u591a\u8fdb\u7a0b\u901a\u4fe1\u5373IPC\u662f\u6307\u4e24\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362;<\/ol>\n
      \u8fdb\u7a0b\u4e00\u822c\u6307\u4e00\u4e2a\u6267\u884c\u5355\u5143\uff0c\u5728PC\u548c\u79fb\u52a8\u8bbe\u5907\u4e2d\u6307\u4e00\u4e2a\u7a0b\u5e8f\u6216\u5e94\u7528;<\/ol>\n
        \u6700\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\uff0cAndroid\u5e94\u7528\u4e2d\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5305\u542b\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5373\u4e3b\u7ebf\u7a0b\uff0c\u4e5f\u53eb\u4f5cUI\u7ebf\u7a0b\uff0c\u53ea\u80fd\u5728\u6b64\u7ebf\u7a0b\u66f4\u65b0\u64cd\u4f5cUI;<\/ol>\n
          \u666e\u901a\u60c5\u51b5\u4e0b\u662f\u4e0d\u9700\u8981\u591a\u8fdb\u7a0b\u7684\uff0c\u4f46\u662f\u5f53\u5e94\u7528\u9700\u8981\u66f4\u591a\u7684\u5185\u5b58\u6216\u8005\u67d0\u4e9b\u7279\u6b8a\u7684Module\u6216\u7279\u6b8a\u7684\u9700\u6c42\u9700\u8981\u8fd0\u884c\u5728\u591a\u8fdb\u7a0b\u6761\u4ef6\u4e0b;<\/ol>\n
          2\u3001\u5f00\u542f\u591a\u8fdb\u7a0b<\/strong><\/span><\/div>\n

          Android\u4e2d\u5f00\u542f\u591a\u8fdb\u7a0b\u53ea\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u5c31\u662f\u5728AndroidManifest.xml\u4e2d\u6ce8\u518cService\u3001Activity\u3001Receiver\u3001ContentProvider\u65f6\u6307\u5b9aandroid:process\u5c5e\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n

          <service \r\n    android:name=\".MyService\" \r\n    android:process=\":remote\">\r\n<\/service>\r\n\r\n<activity \r\n    android:name=\".MyActivity\" \r\n    android:process=\"com.test.remote2\"> \r\n<\/activity>\r\n<\/pre>\n

          \u6211\u4eec\u4e3aMyService\u548cMyActivity\u6307\u5b9a\u7684android:process\u5c5e\u6027\u503c\u6709\u6240\u4e0d\u540c\uff0c\u5b83\u4eec\u7684\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n

            :remote\uff1a\u4ee5:\u5f00\u5934\u662f\u4e00\u79cd\u7b80\u5199\uff0c\u7cfb\u7edf\u4f1a\u5728\u5f53\u524d\u8fdb\u7a0b\u540d\u524d\u9644\u4ef6\u5f53\u524d\u5305\u540d\uff0c\u5b8c\u6574\u7684\u8fdb\u7a0b\u540d\u4e3a\uff1acom.test:remote\uff0c\u540c\u65f6\u4ee5:\u5f00\u5934\u7684\u8fdb\u7a0b\u5c5e\u4e8e\u5f53\u524d\u5e94\u7528\u7684\u79c1\u6709\u8fdb\u7a0b\uff0c\u5176\u5b83\u5e94\u7528\u7684\u7ec4\u4ef6\u4e0d\u80fd\u548c\u5b83\u8dd1\u5728\u540c\u4e00\u8fdb\u7a0b;<\/ol>\n
              com.test.remote2\uff1a\u8fd9\u662f\u5b8c\u6574\u7684\u547d\u540d\u65b9\u5f0f\uff0c\u4e0d\u4f1a\u9644\u52a0\u5305\u540d\uff0c\u5176\u5b83\u5e94\u7528\u5982\u679c\u548c\u8be5\u8fdb\u7a0b\u7684ShareUID\u3001\u7b7e\u540d\u76f8\u540c\uff0c\u5219\u53ef\u4ee5\u548c\u5b83\u8dd1\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5b9e\u73b0\u6570\u636e\u5171\u4eab;<\/ol>\n
              3\u3001Android\u4e2d\u7684\u591a\u8fdb\u7a0b\u901a\u4fe1\u65b9\u5f0f<\/strong><\/span><\/div>\n

              \u591a\u8fdb\u7a0b\u901a\u4fe1\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff0c\u5b83\u4eec\u4e4b\u95f4\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u6839\u636e\u4f7f\u7528\u573a\u666f\u9009\u62e9\u9009\u62e9\uff1a<\/p>\n

                AIDL\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u8fdb\u7a0b\u95f4\u4e00\u5bf9\u591a\u7684\u5b9e\u65f6\u5e76\u53d1\u901a\u4fe1\uff0c\u5e76\u53ef\u5b9e\u73b0 RPC (\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528);<\/ol>\n
                  Messenger\uff1a\u652f\u6301\u4e00\u5bf9\u591a\u7684\u4e32\u884c\u5b9e\u65f6\u901a\u4fe1\uff0c AIDL \u7684\u7b80\u5316\u7248\u672c;<\/ol>\n
                    Bundle\uff1a\u56db\u5927\u7ec4\u4ef6\u7684\u8fdb\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff0c\u53ea\u80fd\u4f20\u8f93 Bundle \u652f\u6301\u7684\u6570\u636e\u7c7b\u578b;<\/ol>\n
                      ContentProvider\uff1a\u5f3a\u5927\u7684\u6570\u636e\u6e90\u8bbf\u95ee\u652f\u6301\uff0c\u4e3b\u8981\u652f\u6301 CRUD \u64cd\u4f5c\uff0c\u4e00\u5bf9\u591a\u7684\u8fdb\u7a0b\u95f4\u6570\u636e\u5171\u4eab\uff0c\u4f8b\u5982\u6211\u4eec\u7684\u5e94\u7528\u8bbf\u95ee\u7cfb\u7edf\u7684\u901a\u8baf\u5f55\u6570\u636e;<\/ol>\n
                        BroadcastReceiver\uff1a\u5373\u5e7f\u64ad\uff0c\u4f46\u53ea\u80fd\u5355\u5411\u901a\u4fe1\uff0c\u63a5\u6536\u8005\u53ea\u80fd\u88ab\u52a8\u7684\u63a5\u6536\u6d88\u606f;<\/ol>\n
                          \u6587\u4ef6\u5171\u4eab\uff1a\u5728\u975e\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b\u5171\u4eab\u7b80\u5355\u7684\u6570\u636e;<\/ol>\n
                            Socket\uff1a\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6570\u636e;<\/ol>\n

                            \"\"<\/p>\n

                            \u4e8c\u3001 \u591a\u8fdb\u7a0b\u5e26\u6765\u7684\u95ee\u9898<\/strong><\/div>\n
                            1\u3001\u9759\u6001\u53d8\u91cf\u5931\u6548<\/strong><\/span><\/div>\n

                            \u5728\u4e00\u4e2aActivity\u4e2d\u65b0\u5efa\u4e00\u4e2a\u9759\u6001\u53d8\u91cfTEST_STATIC\uff0c\u5e76\u5728RemoteActivity1\u4e2d\u7684onStartOtherRemoteActivity\u65b9\u6cd5\u4e2d\u81ea\u589e\uff0c\u4e4b\u540e\u542f\u52a8RemoteActivity2\uff0c\u5e76\u57282\u4e2d\u6253\u5370TEST_STATIC\u7684\u503c;<\/p>\n

                            public static int TEST_STATIC = 21; \r\npublic void onStartOtherRemoteActivity(View view) { \r\n    TEST_STATIC++; \r\n    Log.e(TAG, \"onStartOtherRemoteActivity: \" + TEST_STATIC); \r\n    startActivity(new Intent(this, RemoteActivity2.class)); \r\n} \r\n\u7ed3\u679c\uff1a \r\n\/\/ RemoteActivity1 log \r\nE\/RemoteActivity1: onStartOtherRemoteActivity: 22 \r\n\/\/ RemoteActivity2 log \r\nE\/RemoteActivity2: onCreate: 21<\/pre>\n

                            \u5e76\u4e0d\u76f8\u540c\u7684\u6570\u503c\u8bf4\u660e\u5728\u591a\u8fdb\u7a0b\u4e2d\u9759\u6001\u53d8\u91cf\u662f\u5931\u6548\u7684\uff0c\u540c\u6837\u7684\u56e0\u4e3a\u9759\u6001\u53d8\u91cf\u5e26\u6765\u7684\u95ee\u9898\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u5931\u6548;<\/p>\n

                            \u539f\u56e0\u5c31\u662f\u591a\u8fdb\u7a0b\u65f6Android\u4e3a\u5176\u4ed6\u8fdb\u7a0b\u5206\u914d\u4e86\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u673a\uff0c\u5bfc\u81f4\u4e0d\u540c\u7684\u865a\u62df\u673a\u5728\u5185\u5b58\u4e0a\u6709\u4e0d\u540c\u7684\u5185\u5b58\u5730\u5740\uff0c \u5f53\u5728\u65b0\u7684\u8fdb\u7a0b\u8bbf\u95ee\u53d8\u91cf\u65f6\uff0c\u8bbf\u95ee\u7684\u5176\u5b9e\u662f\u8fd9\u4e2a\u7c7b\u5728\u65b0\u7684\u865a\u62df\u673a\u4e2d\u7684\u526f\u672c\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e\u5728:remote\u548c.remote\u4e2d\u5404\u6709\u4e00\u4e2aRemoteActivity1\u7c7b\uff0c\u800c.remote\u8bbf\u95ee\u7684\u90a3\u4e2a\u526f\u672c\u4e2d\u7684TEST_STATIC\u662f\u6ca1\u6709\u8fdb\u884c\u81ea\u589e\u64cd\u4f5c\u7684\uff0c\u6240\u4ee5\u8fd8\u662f\u4f1a\u6253\u5370\u51fa21\u7684\u521d\u59cb\u6570\u503c\uff0c\u800c\u5728:remote\u4e2d\u662f\u81ea\u589e\u8fc7\u768422;<\/p>\n

                            \u5355\u4f8b\u6a21\u5f0f\u4e5f\u662f\u540c\u6837\u7684\u89e3\u91ca\uff0c\u5f53\u5728\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u8bbf\u95ee\u5355\u4f8b\u7c7b\u65f6\uff0c\u5728\u6b64\u8fdb\u7a0b\u4e2d\u5176\u5b9e\u5e76\u6ca1\u6709\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u624d\u4f1a\u5931\u6548;<\/p>\n

                            2\u3001\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\u5931\u6548<\/strong><\/span><\/div>\n

                            \u672c\u8d28\u4e0a\u8ddf\u9759\u6001\u53d8\u91cf\u7c7b\u4f3c\uff0c\u5728\u4e00\u4e2a\u8fdb\u7a0b\u9501\u4f4f\u7684\u662f\u526f\u672c\u7684\u5bf9\u8c61\uff0c\u800c\u5728\u53e6\u4e00\u4e2a\u526f\u672c\u4e2d\uff0c\u5185\u5b58\u90fd\u4e0d\u540c\uff0c\u6240\u4ee5\u80af\u5b9a\u662f\u65e0\u6548\u7684;<\/p>\n

                            3\u3001SharedPreferences\u53ef\u9760\u6027\u4e0b\u964d<\/strong><\/span><\/div>\n

                            SharedPreferences\u4e0d\u652f\u6301\u4e24\u4e2a\u8fdb\u7a0b\u540c\u65f6\u53bb\u6267\u884c\u5199\u64cd\u4f5c\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u4e00\u5b9a\u51e0\u7387\u7684\u6570\u636e\u4e22\u5931;<\/p>\n

                            SharedPreferences\u7684\u5e95\u5c42\u662f\u901a\u8fc7\u8bfb\u5199XML\u6587\u4ef6\u5b9e\u73b0\u7684\uff0c\u5e76\u53d1\u5199\u5f88\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\uff0c\u5e76\u53d1\u8bfb\u5199\u90fd\u4e0d\u80fd\u4fdd\u8bc1\u4e0d\u4f1a\u51fa\u95ee\u9898;<\/p>\n

                            4\u3001Application\u4f1a\u88ab\u521b\u5efa\u591a\u6b21<\/strong><\/span><\/div>\n

                            \u5f53\u4e00\u4e2a\u7ec4\u4ef6\u8dd1\u5728\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u4e2d\u65f6\uff0c\u7cfb\u7edf\u7ed9\u65b0\u7684\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u673a\uff0c\u5c31\u76f8\u5f53\u4e8e\u5e94\u7528\u53c8\u4e00\u6b21\u7684\u91cd\u65b0\u542f\u52a8\uff0cApplication\u4f5c\u4e3a\u5e94\u7528\u57fa\u7840\u80af\u5b9a\u4e5f\u4f1a\u88ab\u91cd\u65b0\u521b\u5efa;<\/p>\n

                            \u65b0\u5efaApplication\u7c7b\uff0c\u7ee7\u627f\u81eaApplication\uff0c\u5e76\u5728onCreate\u65b9\u6cd5\u4e2d\u8f93\u51fa\u5f53\u524d\u8fdb\u7a0b\u7684PID\uff1a<\/p>\n

                            public class LApplication extends Application { \r\n    private static final String TAG = \"LApplication\"; \r\n    @Override \r\n    public void onCreate() { \r\n        super.onCreate(); \r\n        Log.e(TAG, \"onCreate: \" + android.os.Process.myPid()); \r\n    } \r\n}<\/pre>\n

                            \u5f53\u4f9d\u6b21\u5f00\u542f\u8fdb\u7a0b\u540e\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n

                            \/\/ Main \r\nE\/LApplication: onCreate: 16031 \r\n\/\/ RemoteActivity1 \r\nE\/LApplication: onCreate: 16127 \r\n\/\/ RemoteActivity2 \r\nE\/LApplication: onCreate: 16202<\/pre>\n

                            Application\u88ab\u521b\u5efa\u591a\u6b21\u5e26\u6765\u7684\u95ee\u9898\u662f\uff0c\u6709\u4e9b\u65f6\u5019\u4f1a\u9700\u8981\u5728Application\u4e2d\u521d\u59cb\u5316\u4e9b\u4f9d\u8d56\uff0c\u4f46\u662f\u591a\u8fdb\u7a0b\u5c31\u4f1a\u968f\u7740Application\u7684\u521b\u5efa\u800c\u91cd\u590d\u521d\u59cb\u5316\uff0c\u53ef\u4ee5\u5728Application\u4e2d\u8bbe\u7f6e\u4e00\u4e9b\u6761\u4ef6\u8df3\u8fc7\u91cd\u590d\u521d\u59cb\u5316\u90e8\u5206;<\/p>\n

                            \/\/ \u6839\u636epid\u83b7\u53d6\u8fdb\u7a0b\u540d \r\nprivate String getAppName(int pid) { \r\n    String processName = null; \r\n    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); \r\n    List list = am.getRunningAppProcesses(); \r\n    for (ActivityManager.RunningAppProcessInfo info : list) { \r\n        try { \r\n            if (info.pid == pid) { \r\n                processName = info.processName; \r\n                return processName; \r\n            } \r\n        } catch (Exception e) { \r\n            e.printStackTrace(); \r\n            return null; \r\n        } \r\n    } \r\n    return null; \r\n}<\/pre>\n

                            \u901a\u8fc7PID\u83b7\u53d6\u8fdb\u7a0b\u540d\uff0c\u4e0e\u5305\u540d\u505a\u5bf9\u6bd4\uff0c\u53ea\u6709\u8ddf\u5305\u540d\u4e00\u81f4\u65f6\u624d\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c;<\/p>\n","protected":false},"excerpt":{"rendered":"

                            Android\u7684\u591a\u8fdb\u7a0b\u901a\u4fe1\u5373IPC\u662f\u6307\u4e24\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362; \u8fdb\u7a0b\u4e00\u822c\u6307\u4e00\u4e2a\u6267\u884c\u5355\u5143\uff0c\u5728PC\u548c\u79fb\u52a8\u8bbe\u5907\u4e2d\u6307 […]<\/p>\n","protected":false},"author":1329,"featured_media":230000,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-229993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/229993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=229993"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/229993\/revisions"}],"predecessor-version":[{"id":230001,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/229993\/revisions\/230001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/230000"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=229993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=229993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=229993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}