{"id":2964,"date":"2020-09-23T16:17:19","date_gmt":"2020-09-23T08:17:19","guid":{"rendered":"https:\/\/learn-house.idv.tw\/?p=2964"},"modified":"2021-02-09T14:29:21","modified_gmt":"2021-02-09T06:29:21","slug":"%e7%b0%a1%e6%98%93%e7%90%86%e8%a7%a3android-binder","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=2964","title":{"rendered":"\u7c21\u6613\u7406\u89e3Android Binder"},"content":{"rendered":"<p>\u7531\u65bc\u4e00\u76f4\u641e\u4e0d\u61c2Android Binder\uff0c\u53ea\u77e5\u9053\u662f\u4e00\u500bIPC\u6a5f\u5236\u3002\u7db2\u8def\u4e0a\u770b\u4e86\u5f88\u591a\u7bc7\uff0c\u4f46\u5230\u6700\u5f8c\u90fd\u4f3c\u61c2\u975e\u61c2\uff0c\u7136\u5f8c\u5c31\u5fd8\u4e86\u5230\u5e95\u5728\u8b1b\u5565\u3002<br \/>\n\u4e00\u76f4\u5230\u770b\u5230\u5c0d\u5cb8\u4e2d\u570b\u7684\u4e00\u7bc7\u6587\u7ae0\uff0c\u89ba\u5f97\u7c21\u55ae\u660e\u77ad\uff0c\u70ba\u4e86\u9810\u9632\u4e4b\u5f8c\u9023\u7d50\u5931\u6548\uff0c\u5728\u9019\u88e1\u5099\u4efd\u4e00\u4e0b<br \/>\n\u539f\u6587\u7db2\u5740\uff1ahttps:\/\/kknews.cc\/code\/z2vgzl.html<br \/>\n\u5982\u679c\u60f3\u8981\u5fb9\u5e95\u4e86\u89e3\u5462\uff0c\u5247\u53ef\u4ee5\u770b\u770b\u9019\u7bc7\uff1ahttps:\/\/zhuanlan.zhihu.com\/p\/35519585<br \/>\n<!--more--><\/p>\n<p>\u672c\u6587\u5206\u70ba\u4ee5\u4e0b\u5e7e\u500b\u90e8\u5206\u53bb\u4ecb\u7d39<\/p>\n<ul>\n<li>Android \u6574\u9ad4\u67b6\u69cb<\/li>\n<li>Binder IPC \u7684\u67b6\u69cb<\/li>\n<li>\u624b\u52d5\u5be6\u73fe Binder IPC<\/li>\n<li>\u4f7f\u7528 AIDL \u5be6\u73fe Binder IPC<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u89ba\u5f97\u6587\u7ae0\u592a\u9577\uff0c\u53ef\u4ee5\u5148\u53ea\u770b\u300c\u5c0f\u7d50\u300d\u90e8\u5206\uff0c\u5c0f\u7d50\u6703\u628a\u6bcf\u500b\u90e8\u5206\u7684\u91cd\u9ede\u7e3d\u7d50\u51fa\u4f86\uff0c\u6709\u4e9b\u90e8\u5206\u53ef\u4ee5\u8df3\u904e\u3002<\/p>\n<p><strong>\u4e00\uff0cAndroid \u6574\u9ad4\u67b6\u69cb<\/strong><\/p>\n<p>\u4e0d\u8b58\u5eec\u5c71\u771f\u9762\u76ee\uff0c\u53ea\u7de3\u8eab\u5728\u6b64\u5c71\u4e2d\uff0c\u6240\u4ee5\u6211\u5011\u5148\u4f86\u5927\u6982\u770b\u4e0b Android \u9019\u5ea7\u5927\u5c71\u7684\u6574\u9ad4\u8f2a\u5ed3\u3002\u6211\u5011\u5148\u5f9e Android \u7684\u6574\u9ad4\u67b6\u69cb\u4f86\u770b\u770b Binder \u662f\u8655\u65bc\u4ec0\u9ebc\u5730\u4f4d\uff0c\u9019\u5f35\u5716\u5f15\u81ea Android \u9805\u76ee\u958b\u6e90\u7db2\u7ad9\uff1ahttps:\/\/source.android.com<strong><br \/>\n<\/strong><\/p>\n<figure class=\"img-container\"><span class=\"img-responsive aligncenter lazy-img loaded\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/lh3.googleusercontent.com\/pw\/ACtC-3d8C1kQjCHCf4yJ38SGvpIrrSEPsIIWxJY3slvQXeP31ozIMPYaY1qGur9gXoq0-gOVIu5OkNHn2jta52jWDmh6V8O1NW9OXjNxM69p602oCZKzfMv9lxWkVGqdyfEAknw3RIcYhYDNPJ2gwT2lCdp3bQ=w486-h740-no?authuser=0\" width=\"486\" height=\"740\" \/><\/span><\/figure>\n<p>\u5f9e\u4e0b\u5f80\u4e0a\u4f9d\u6b21\u70ba<\/p>\n<ul class=\" list-paddingleft-2\">\n<li>\u5167\u6838\u5c64\uff1aLinux \u5167\u6838\u548c\u5404\u985e\u786c\u9ad4\u8a2d\u5099\u7684\u9a45\u52d5\uff0c\u9019\u88e1\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cBinder IPC \u9a45\u52d5\u4e5f\u662f\u5728\u9019\u4e00\u5c64\u5be6\u73fe\uff0c\u6bd4\u8f03\u7279\u6b8a<\/li>\n<li>\u786c\u9ad4\u62bd\u8c61\u5c64\uff1a\u5c01\u88dd\u300c\u5167\u6838\u5c64\u300d\u786c\u9ad4\u9a45\u52d5\uff0c\u63d0\u4f9b\u53ef\u4f9b\u300c\u7cfb\u7d71\u670d\u52d9\u5c64\u300d\u8abf\u7528\u7684\u7d71\u4e00\u786c\u9ad4\u63a5\u53e3<\/li>\n<li>\u7cfb\u7d71\u670d\u52d9\u5c64\uff1a\u63d0\u4f9b\u6838\u5fc3\u670d\u52d9\uff0c\u4e26\u4e14\u63d0\u4f9b\u53ef\u4f9b\u300c\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64\u300d\u8abf\u7528\u7684\u63a5\u53e3<\/li>\n<li>Binder IPC \u5c64\uff1a\u4f5c\u70ba\u300c\u7cfb\u7d71\u670d\u52d9\u5c64\u300d\u8207\u300c\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64\u300d\u7684 IPC \u6a4b\u6a11\uff0c\u4e92\u76f8\u50b3\u905e\u63a5\u53e3\u8abf\u7528\u7684\u6578\u64da\uff0c\u5be6\u73fe\u8de8\u9032\u5c64\u7684\u901a\u8a0a<\/li>\n<li>\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64\uff1a\u9019\u4e00\u5c64\u53ef\u4ee5\u7406\u89e3\u70ba Android SDK\uff0c\u63d0\u4f9b\u56db\u5927\u7d44\u4ef6\uff0cView \u7e6a\u88fd\u9ad4\u7cfb\u7b49\u5e73\u6642\u958b\u767c\u4e2d\u7528\u5230\u7684\u57fa\u790e\u90e8\u4ef6<\/li>\n<\/ul>\n<p>\u5728\u4e00\u500b\u5927\u7684\u9805\u76ee\u88e1\u9762\uff0c\u5206\u5c64\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u8655\u65bc\u6700\u5e95\u5c64\u7684\u63a5\u53e3\u6700\u5177\u6709\u300c\u901a\u7528\u6027\u300d\uff0c\u63a5\u53e3\u7c92\u5ea6\u6700\u7d30\uff0c\u8d8a\u5f80\u4e0a\u5c64\u901a\u7528\u6027\u964d\u4f4e\u3002\u7406\u8ad6\u4e0a\u4f86\u8aaa\u4e0a\u9762\u7684\u6bcf\u4e00\u5c64\u90fd\u53ef\u4ee5\u300c\u958b\u653e\u300d\u7d66\u958b\u767c\u8005\u8abf\u7528\uff0c\u4f8b\u5982\u958b\u767c\u8005\u53ef\u4ee5\u76f4\u63a5\u8abf\u7528\u786c\u9ad4\u62bd\u8c61\u5c64\u7684\u63a5\u53e3\u53bb\u64cd\u4f5c\u786c\u9ad4\uff0c\u6216\u8005\u76f4\u63a5\u8abf\u7528\u7cfb\u7d71\u670d\u52d9\u5c64\u4e2d\u7684\u63a5\u53e3\u53bb\u76f4\u63a5\u4f5c\u696d\u7cfb\u7d71\u670d\u52d9\uff0c\u751a\u81f3\u662f\u50cf Windows \u958b\u767c\u4e00\u6a23\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u5167\u6838\u5c64\u5beb\u7a0b\u5e8f\uff0c\u904b\u884c\u5728\u5167\u6838\u4e2d\u3002\u4e0d\u904e\u958b\u653e\u5e36\u4f86\u7684\u554f\u984c\u5c31\u662f\u958b\u767c\u8005\u6b0a\u5229\u592a\u5927\uff0c\u5c0d\u65bc\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u662f\u6c92\u6709\u4efb\u4f55\u597d\u8655\u7684\uff0c\u4e00\u500b\u75c5\u6bd2\u88fd\u4f5c\u8005\u5beb\u4e86\u4e00\u500b\u5167\u6838\u5c64\u7684\u75c5\u6bd2\uff0c\u7cfb\u7d71\u4e5f\u8a31\u6c38\u9060\u4e5f\u8d77\u4e0d\u4f86\u4e86\u3002\u6240\u4ee5\u8c37\u6b4c\u7684\u505a\u6cd5\u662f\u5c07\u958b\u767c\u8005\u7684\u6b0a\u5229\u6536\u650f\u5230\u4e86\u300c\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64\u300d\uff0c\u958b\u767c\u8005\u53ea\u80fd\u8abf\u7528\u9019\u4e00\u5c64\u63d0\u4f9b\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u5c64\u6b21\u4e2d\uff0c\u5167\u6838\u5c64\u8207\u786c\u9ad4\u62bd\u8c61\u5c64\u5747\u7528 C\/C++ \u5be6\u73fe\uff0c\u7cfb\u7d71\u670d\u52d9\u5c64\u662f\u4ee5 Java \u5be6\u73fe\uff0c\u786c\u9ad4\u62bd\u8c61\u5c64\u7de8\u8b6f\u70ba so \u6587\u4ef6\uff0c\u4ee5 JNI \u7684\u5f62\u5f0f\u4f9b\u7cfb\u7d71\u670d\u52d9\u5c64\u4f7f\u7528\u3002\u7cfb\u7d71\u670d\u52d9\u5c64\u4e2d\u7684\u670d\u52d9\u96a8\u7cfb\u7d71\u7684\u555f\u52d5\u800c\u555f\u52d5\uff0c\u53ea\u8981\u4e0d\u95dc\u6a5f\uff0c\u5c31\u6703\u4e00\u76f4\u904b\u884c\u3002\u9019\u4e9b\u670d\u52d9\u5e79\u4ec0\u9ebc\u4e8b\u60c5\u5462\uff1f\u5176\u5be6\u5f88\u7c21\u55ae\uff0c\u5c31\u662f\u5b8c\u6210\u4e00\u500b\u624b\u6a5f\u8a72\u6709\u7684\u6838\u5fc3\u529f\u80fd\u5982\u7c21\u8a0a\u7684\u6536\u767c\u7ba1\u7406\u3001\u96fb\u8a71\u7684\u63a5\u807d\u3001\u639b\u65b7\u4ee5\u53ca\u61c9\u7528\u7a0b\u5f0f\u7684\u5305\u7ba1\u7406\u3001Activity \u7684\u7ba1\u7406\u7b49\u7b49\u3002\u6bcf\u4e00\u500b\u670d\u52d9\u5747\u904b\u884c\u5728\u4e00\u500b\u7368\u7acb\u9032\u7a0b\u4e2d\uff0c\u56e0\u70ba\u662f\u4ee5 Java \u5be6\u73fe\uff0c\u6240\u4ee5\u672c\u8cea\u4e0a\u4f86\u8aaa\u5c31\u662f\u904b\u884c\u5728\u4e00\u500b\u7368\u7acb\u9032\u7a0b\u7684 Dalvik \u865b\u64ec\u6a5f\u4e2d\u3002\u554f\u984c\u5c31\u4f86\u4e86\uff0c\u958b\u767c\u8005\u7684 APP \u904b\u884c\u5728\u4e00\u500b\u65b0\u7684\u9032\u7a0b\u7a7a\u9593\uff0c\u5982\u4f55\u8abf\u7528\u5230\u7cfb\u7d71\u670d\u52d9\u5c64\u4e2d\u7684\u63a5\u53e3\u5462\uff1f\u7b54\u6848\u662f IPC\uff08Inter-Process Communication\uff09\uff0c\u9032\u7a0b\u9593\u901a\u8a0a\uff0c\u7e2e\u5beb\u8207 RPC\uff08Remote Procedure Call\uff09\u662f\u4e0d\u4e00\u6a23\u7684\uff0c\u5be6\u73fe\u539f\u7406\u4e5f\u662f\u4e0d\u4e00\u6a23\u7684\u3002\u6bcf\u4e00\u500b\u7cfb\u7d71\u670d\u52d9\u5728\u61c9\u7528\u5c64\u5e8f\u6846\u67b6\u5c64\u90fd\u6709\u4e00\u500b Manager \u8207\u4e4b\u5c0d\u61c9\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u8abf\u7528\u5176\u76f8\u95dc\u7684\u529f\u80fd\uff0c\u5177\u9ad4\u95dc\u4fc2\u5927\u81f4\u5982\u4e0b<\/p>\n<figure class=\"img-container\"><span class=\"img-responsive aligncenter lazy-img loaded\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone \" src=\"https:\/\/lh3.googleusercontent.com\/pw\/ACtC-3cgVow84J5nu12u7OKG0IX1XDlmpTAcsHTmGMkaMdxi-Cvb0463YY1uoQDkea3-P6qbigKZhdnwNaQqLCdUVijebvUmpuVFFi7oLpl1NgEJ8dgaVBmuBnoPdtJdmdnM4t25Sc9zdp5xJsrGBC1uQfLZEw=w640-h447-no?authuser=0\" width=\"534\" height=\"373\" \/><\/span><\/figure>\n<p>IPC \u7684\u65b9\u5f0f\u6709\u5f88\u591a\u7a2e\uff0c\u4f8b\u5982 socket\u3001\u5171\u4eab\u5167\u5b58\u3001\u7ba1\u9053\u3001\u6d88\u606f\u968a\u5217\u7b49\u7b49\uff0c\u6211\u5011\u5c31\u4e0d\u53bb\u6df1\u7a76\u70ba\u4f55\u8981\u4f7f\u7528 Binder \u800c\u4e0d\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u53bb\u505a\uff0c\u5230\u76ee\u524d\u70ba\u6b62\uff0c\u9019\u5ea7\u5927\u5c71\u7684\u9762\u76ee\u7b97\u662f\u6709\u500b\u5927\u6982\u7684\u8f2a\u5ed3\u4e86\u3002<\/p>\n<p><strong>1.1 \u5c0f\u7d50<\/strong><\/p>\n<ul class=\" list-paddingleft-2\">\n<li>Android \u5f9e\u4e0b\u800c\u4e0a\u5206\u4e86\u5167\u6838\u5c64\u3001\u786c\u9ad4\u62bd\u8c61\u5c64\u3001\u7cfb\u7d71\u670d\u52d9\u5c64\u3001Binder IPC \u5c64\u3001\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64<\/li>\n<li>Android \u4e2d\u300c\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64\u300d\u4ee5 SDK \u7684\u5f62\u5f0f\u958b\u653e\u7d66\u958b\u767c\u8005\u4f7f\u7528\uff0c\u300c\u7cfb\u7d71\u670d\u52d9\u5c64\u300d\u4e2d\u7684\u6838\u5fc3\u670d\u52d9\u96a8\u7cfb\u7d71\u555f\u52d5\u800c\u904b\u884c\uff0c\u901a\u904e\u61c9\u7528\u5c64\u5e8f\u6846\u67b6\u5c64\u63d0\u4f9b\u7684 Manager \u5be6\u6642\u70ba\u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u670d\u52d9\u8abf\u7528\u3002\u7cfb\u7d71\u670d\u52d9\u5c64\u4e2d\u6bcf\u4e00\u500b\u670d\u52d9\u904b\u884c\u5728\u81ea\u5df1\u7368\u7acb\u7684\u9032\u7a0b\u7a7a\u9593\u4e2d\uff0c\u61c9\u7528\u7a0b\u5f0f\u6846\u67b6\u5c64\u4e2d\u7684 Manager \u901a\u904e Binder IPC \u7684\u65b9\u5f0f\u8abf\u7528\u7cfb\u7d71\u670d\u52d9\u5c64\u4e2d\u7684\u670d\u52d9\u3002<\/li>\n<\/ul>\n<p><strong>\u4e8c\uff0cBinder IPC \u7684\u67b6\u69cb<\/strong><\/p>\n<p>\u4e0b\u9762\u6211\u5011\u5c31\u4f86\u770b\u770b Binder IPC \u7684\u67b6\u69cb\u662f\u600e\u6a23\u7684<\/p>\n<figure class=\"img-container\"><span class=\"img-responsive aligncenter lazy-img loaded\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone \" src=\"https:\/\/lh3.googleusercontent.com\/pw\/ACtC-3dkBN3mz2flPaP58dLwape5v1NtSWLdvtCYCQE5HVsbsQ9QzbPISEfhARvqo10qTTe8MnXloSHh-JcSDftBeiYhIkaTsqnvAJxKQfz7JjdgQU0eRvuqVpRgtp_3Z_BDT56a_yWqixAYglVkjLQhqRBmgA=w640-h346-no?authuser=0\" width=\"535\" height=\"289\" \/><\/span><\/figure>\n<p>Binder IPC \u5c6c\u65bc C\/S \u7d50\u69cb\uff0cClient \u90e8\u5206\u662f\u7528\u6236\u4ee3\u78bc\uff0c\u7528\u6236\u4ee3\u78bc\u6700\u7d42\u6703\u8abf\u7528 Binder Driver \u7684 transact \u63a5\u53e3\uff0cBinder Driver \u6703\u8abf\u7528 Server\uff0c\u9019\u88e1\u7684 Server \u8207 service \u4e0d\u540c\uff0c\u53ef\u4ee5\u7406\u89e3\u70ba Service \u4e2d onBind \u8fd4\u56de\u7684 Binder \u5c0d\u8c61\uff0c\u8acb\u6ce8\u610f\u5340\u5206\u4e0b\u3002<\/p>\n<ul class=\" list-paddingleft-2\">\n<li>Client\uff1a\u7528\u6236\u9700\u8981\u5be6\u73fe\u7684\u4ee3\u78bc\uff0c\u5982 AIDL \u81ea\u52d5\u751f\u6210\u7684\u63a5\u53e3\u985e<\/li>\n<li>Binder Driver\uff1a\u5728\u5167\u6838\u5c64\u5be6\u73fe\u7684 Driver<\/li>\n<li>Server\uff1a\u9019\u500b Server \u5c31\u662f Service \u4e2d onBind \u8fd4\u56de\u7684 IBinder \u5c0d\u8c61<\/li>\n<\/ul>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u9762\u7da0\u8272\u7684\u8272\u584a\u90e8\u5206\u90fd\u662f\u5c6c\u65bc\u7528\u6236\u9700\u8981\u5be6\u73fe\u7684\u90e8\u5206\uff0c\u800c\u85cd\u8272\u90e8\u5206\u662f\u7cfb\u7d71\u53bb\u5be6\u73fe\u4e86\u3002\u4e5f\u5c31\u662f\u8aaa Binder Driver \u9019\u584a\u4e26\u4e0d\u9700\u8981\u77e5\u9053\uff0cServer \u4e2d\u6703\u958b\u555f\u4e00\u500b\u7dda\u7a0b\u6c60\u53bb\u8655\u7406\u5ba2\u6236\u7aef\u8abf\u7528\u3002\u70ba\u4ec0\u9ebc\u8981\u7528\u7dda\u7a0b\u6c60\u800c\u4e0d\u662f\u4e00\u500b\u55ae\u7dda\u7a0b\u968a\u5217\u5462\uff1f\u8a66\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u7528\u55ae\u7dda\u7a0b\u968a\u5217\uff0c\u5247\u6703\u6709\u4efb\u52d9\u7a4d\u58d3\uff0c\u591a\u500b\u5ba2\u6236\u7aef\u540c\u6642\u8abf\u7528\u4e00\u500b\u670d\u52d9\u7684\u6642\u5019\u5c31\u6703\u6709\u4f86\u4e0d\u53ca\u97ff\u61c9\u7684\u60c5\u6cc1\u767c\u751f\uff0c\u9019\u662f\u7d55\u5c0d\u4e0d\u5141\u8a31\u7684\u3002<\/p>\n<p>\u5c0d\u65bc\u8abf\u7528 Binder Driver \u4e2d\u7684 transact \u63a5\u53e3\uff0c\u5ba2\u6236\u7aef\u53ef\u4ee5\u624b\u52d5\u8abf\u7528\uff0c\u4e5f\u53ef\u4ee5\u901a\u904e AIDL \u7684\u65b9\u5f0f\u751f\u6210\u7684\u4ee3\u7406\u985e\u4f86\u8abf\u7528\uff0c\u670d\u52d9\u7aef\u53ef\u4ee5\u7e7c\u627f Binder \u5c0d\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7e7c\u627f AIDL \u751f\u6210\u7684\u63a5\u53e3\u985e\u7684 Stub \u5c0d\u8c61\u3002\u9019\u4e9b\u7d30\u7bc0\u4e0b\u9762\u7e7c\u7e8c\u63a5\u8457\u8aaa\uff0c\u9019\u88e1\u66ab\u6642\u4e0d\u5c55\u958b\u3002<\/p>\n<p>\u5207\u8a18\uff0c\u9019\u88e1 Server \u7684\u5be6\u73fe\u662f\u7dda\u7a0b\u6c60\u7684\u65b9\u5f0f\uff0c\u800c\u4e0d\u662f\u55ae\u7dda\u7a0b\u968a\u5217\u7684\u65b9\u5f0f\uff0c\u5340\u5225\u5728\u65bc\uff0c\u55ae\u7dda\u7a0b\u968a\u5217\u7684\u8a71\uff0cServer \u7684\u4ee3\u78bc\u662f\u7dda\u7a0b\u5b89\u5168\u7684\uff0c\u7dda\u7a0b\u6c60\u7684\u8a71\uff0cServer \u7684\u4ee3\u78bc\u5247\u4e0d\u662f\u7dda\u7a0b\u5b89\u5168\u7684\uff0c\u9700\u8981\u958b\u767c\u8005\u81ea\u5df1\u505a\u597d\u591a\u7dda\u7a0b\u540c\u6b65\u3002<\/p>\n<p><strong>2.1 \u5c0f\u7d50<\/strong><\/p>\n<ul class=\" list-paddingleft-2\">\n<li>Binder IPC \u5c6c\u65bc C\/S \u67b6\u69cb\uff0c\u5305\u62ec Client\u3001Driver\u3001Server \u4e09\u500b\u90e8\u5206<\/li>\n<li>Client \u53ef\u4ee5\u624b\u52d5\u8abf\u7528 Driver \u7684 transact \u63a5\u53e3\uff0c\u4e5f\u53ef\u4ee5\u901a\u904e AIDL \u751f\u6210\u7684 Proxy \u8abf\u7528<\/li>\n<li>Server \u4e2d\u6703\u555f\u52d5\u4e00\u500b\u300c\u7dda\u7a0b\u6c60\u300d\u4f86\u8655\u7406 Client \u7684\u8abf\u7528\u8acb\u6c42\uff0c\u8655\u7406\u5b8c\u6210\u5f8c\u5c07\u7d50\u679c\u8fd4\u56de\u7d66 Driver\uff0cDriver \u518d\u8fd4\u56de\u7d66 Client<\/li>\n<\/ul>\n<p>\u9019\u88e1\u5c31\u56de\u7b54\u4e86\u958b\u7bc7\u63d0\u554f\u7684\u5169\u500b\u554f\u984c\uff1aService \u4e2d\u901a\u904e AIDL \u63d0\u4f9b\u7684\u63a5\u53e3\u4e26\u4e0d\u662f\u7dda\u7a0b\u5b89\u5168\u7684\uff0c\u540c\u7406 ContentProvider \u5e95\u5c64\u4e5f\u662f\u4f7f\u7528 Binder\uff0c\u540c\u6a23\u4e0d\u662f\u7dda\u7a0b\u5b89\u5168\u7684\uff0c\u81f3\u65bc\u662f\u5426\u9700\u8981\u505a\u591a\u7dda\u7a0b\u4fdd\u8b77\uff0c\u770b\u696d\u52d9\u800c\u5b9a\uff0c\u6700\u597d\u662f\u505a\u597d\u591a\u7dda\u7a0b\u540c\u6b65\uff0c\u4ee5\u9632\u842c\u4e00\u3002<\/p>\n<p><strong>\u4e09\uff0c\u624b\u52d5\u5be6\u73fe Binder IPC<\/strong><\/p>\n<p>\u901a\u904e\u4e0a\u9762\u7684\u8b1b\u89e3\uff0c\u5927\u5bb6\u61c9\u8a72\u5c0d\u6574\u9ad4\u7684\u6d41\u7a0b\u5df2\u7d93\u6709\u4e86\u6e05\u695a\u7684\u8a8d\u8b58\uff0c\u4e0b\u9762\u6211\u5011\u5148\u4f86\u770b\u770b\u5982\u4f55\u624b\u52d5\u5be6\u73fe Binder IPC\uff0c\u5373\u4e0d\u4f7f\u7528 AIDL \u7684\u65b9\u5f0f\u3002\u5c0d\u61c9\u4e0a\u9762\u7684 Client\u3001Driver\u3001Server\uff0c\u5728 Activity\u3001Service \u4e2d\u5206\u5225\u662f\u4ec0\u9ebc\u5462\uff1f<\/p>\n<p>\u4e0a\u6587\u8aaa\u7684 Server \u5176\u5be6\u5c31\u662f Service \u4e2d onBind \u8fd4\u56de\u7684 IBinder \u5c0d\u8c61\u3002<\/p>\n<p><strong>3.1 Server<\/strong><\/p>\n<p>\u5047\u5982\u6211\u5011\u8981\u505a\u4e00\u500b\u4e0a\u5831\u6578\u64da\u7684\u529f\u80fd\uff0c\u904b\u884c\u5728 Service \u4e2d\uff0c\u5728\u5f8c\u53f0\u4e0a\u5831\u6578\u64da\uff0c\u63a5\u53e3\u5b9a\u7fa9\u5982\u4e0b<\/p>\n<p>[java]<br \/>\npublic interface IReporter {<br \/>\n   int report(String values, int type);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u90a3\u5982\u4f55\u62ff\u5230\u5b83\u7684 Server \u5c0d\u8c61\u5462\uff1f\u7b54\u6848\u662f\u901a\u904e Service \u7684 onBind \u65b9\u6cd5\u8fd4\u56de\uff0c\u5be6\u73fe\u5982\u4e0b<\/p>\n<p>BinderService.java<\/p>\n<p>[java]<br \/>\npublic class BinderService extends Service {<br \/>\n    public static final int REPORT_CODE = 0;<\/p>\n<p>    public interface IReporter {<br \/>\n        int report(String values, int type);<br \/>\n    }<\/p>\n<p>    public final class Reporter extends Binder implements IReporter {<br \/>\n        @Override<br \/>\n        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {<\/p>\n<p>            return super.onTransact(code, data, reply, flags);<br \/>\n        }<\/p>\n<p>        @Override<br \/>\n        public int report(String values, int type) {<br \/>\n            return type;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    private Reporter mReporter;<\/p>\n<p>    public BinderService() {<br \/>\n        mReporter = new Reporter();<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public IBinder onBind(Intent intent) {<br \/>\n        return mReporter;<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u9019\u88e1\u6211\u66ab\u6642\u4e0d\u5beb onTransact \u7684\u5be6\u73fe\u90e8\u5206\uff0c\u6700\u4e3b\u8981\u7684\u662f\u7e7c\u627f Binder \u5c0d\u8c61\uff0c\u9019\u500b\u662f Android SDK \u63d0\u4f9b\u7684\u57fa\u985e\uff0c\u5b83\u5be6\u73fe\u4e86 IBinder \u63a5\u53e3\uff0c\u4e26\u4e14\u5c01\u88dd\u4e86\u5e95\u5c64\u7684 Binder Driver\uff0c\u770b\u770b\u5b83\u662f\u5982\u4f55\u521d\u59cb\u5316\u7684<\/p>\n<p>Binder.java<\/p>\n<p>[java]<br \/>\npublic Binder() {<br \/>\n  init();<br \/>\n  &#8230;<br \/>\n}<br \/>\n&#8230;<br \/>\nprivate native final void init();<br \/>\n[\/java]<\/p>\n<p>\u9019\u88e1\u8abf\u7528 native \u7684\u4e00\u500b init \u65b9\u6cd5\uff0c\u6211\u5011\u5c31\u4e0d\u53bb\u6df1\u7a76\u4e86\uff0c\u77e5\u9053\u5b83\u662f\u5c0d\u5e95\u5c64\u7684 Binder Driver \u7684\u5c01\u88dd\u5373\u53ef\u3002\u7576\u5ba2\u6236\u7aef\u767c\u8d77\u8acb\u6c42\u7684\u6642\u5019\uff0cBinder Driver \u6703\u8abf\u7528\u5b83\u7684 execTransact \u65b9\u6cd5\uff0c\u4e26\u5728\u5167\u90e8\u8abf\u7528\u5230 onTransact \u65b9\u6cd5\uff0c\u7528\u6236\u7aef\u4ee3\u78bc\u53ef\u4ee5\u91cd\u8f09\u8a72\u65b9\u6cd5\u53bb\u5be6\u73fe\u81ea\u5df1\u7684\u696d\u52d9\u908f\u8f2f\u4ee3\u78bc\u3002\u6211\u5011\u7684\u5be6\u73fe\u65b9\u5f0f\u5982\u4e0b<\/p>\n<p>BinderService.java<\/p>\n<p>[java]<br \/>\nprotected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {<br \/>\n    switch (code) {<br \/>\n        case REPORT_CODE:<br \/>\n            data.enforceInterface(&quot;reporter&quot;);<br \/>\n            String values = data.readString();<br \/>\n            Log.i(&quot;IReporter&quot;, &quot;data is &#8216;&quot; + values + &quot;&#8217;&quot;);<br \/>\n            int type = data.readInt();<br \/>\n            int result = report(values, type);<br \/>\n            reply.writeInterfaceToken(&quot;reporter&quot;);<br \/>\n            reply.writeInt(result);<br \/>\n            return true;<br \/>\n    }<br \/>\n    return super.onTransact(code, data, reply, flags);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u9019\u88e1\u7684\u4e3b\u8981\u904e\u7a0b\u5c31\u662f\uff1a\u7372\u53d6\u5230 data \u4e2d\u50b3\u905e\u904e\u4f86\u7684\u53c3\u6578 values \u548c type\uff0c\u8abf\u7528\u81ea\u5df1\u5be6\u73fe\u7684 report \u51fd\u6578\uff0c\u5c07\u8fd4\u56de\u503c\u5beb\u5230 reply \u4e2d\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u9019\u88e1\u5be6\u73fe\u7684\u5169\u500b\u95dc\u9375\u9ede\u5c31\u662f<\/p>\n<ul class=\" list-paddingleft-2\">\n<li>Reporter \u985e\u7e7c\u627f Binder \u985e\uff0c\u91cd\u8f09 onTransact \u51fd\u6578\uff0c\u5be6\u73fe\u81ea\u5df1\u7684\u696d\u52d9\u908f\u8f2f<\/li>\n<li>\u5728 Service \u7684 onBind \u4e2d\u8fd4\u56de Reporter \u985e\u7684\u5be6\u4f8b<\/li>\n<\/ul>\n<p>\u9019\u88e1\u770b\u4e0d\u5230\u534a\u9ede\u7dda\u7a0b\u6c60\u7684\u5f71\u5b50\u5c0d\u5427\uff0c\u5176\u5be6\u662f\u5728 Binder \u5167\u90e8\u7684 native \u65b9\u6cd5\u4e2d\u53bb\u5be6\u73fe\u4e86\u7684\uff0c\u8a18\u4f4f\u4f60\u5beb\u7684\u4ee3\u78bc\u8981\u4fdd\u6301\u7dda\u7a0b\u5b89\u5168\u5c31\u5c0d\u4e86\u3002<\/p>\n<p><strong>3.2 Driver<\/strong><\/p>\n<p>\u8a72\u90e8\u5206\u5df2\u7d93\u88ab Binder \u985e\u7d66\u5c01\u88dd\u4e86\uff0c\u66b4\u9732\u7d66\u958b\u767c\u8005\u7684\u5df2\u7d93\u662f\u5f88\u7c21\u55ae\u7684\u4f7f\u7528\u65b9\u5f0f\u4e86\uff0c\u5373\u7e7c\u627f Binder\uff0c\u5be6\u73fe onTransact \u5373\u53ef\u3002<\/p>\n<p><strong>3.3 Client<\/strong><\/p>\n<p>\u90a3 Client \u662f\u4ec0\u9ebc\u5462\uff1f\u4e5f\u5c31\u662f\u6211\u5011\u60f3\u4f7f\u7528 IReport \u63a5\u53e3\u4f86\u505a\u6578\u64da\u4e0a\u5831\u7684\u5730\u65b9\uff0c\u4e00\u822c\u90fd\u5728 Activity \u88e1\u9762\uff0c\u4e3b\u8981\u5be6\u73fe\u5982\u4e0b<\/p>\n<p>MainActivity.java<\/p>\n<p>[java]<br \/>\nprivate IBinder mReporterBind;<br \/>\nprivate class BindConnection implements ServiceConnection {<br \/>\n    @Override<br \/>\n    public void onServiceConnected(ComponentName name, IBinder service) {<br \/>\n        mReporterBind = service;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public void onServiceDisconnected(ComponentName name) {<br \/>\n        mReporterBind = null;<br \/>\n    }<br \/>\n}<\/p>\n<p>&#8230;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\n    Intent intent = new Intent(this, BinderService.class);<br \/>\n    bindService(intent, new BindConnection(), BIND_AUTO_CREATE);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u9019\u6a23\u5c31\u62ff\u5230\u4e86\u5f8c\u53f0 Service \u4e2d\u7684 onBind \u8fd4\u56de\u7684 IBinder \u5c0d\u8c61\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762 Binder IPC \u67b6\u69cb\u4e2d\u7684 mRemote \u5c0d\u8c61\u3002\u81f3\u65bc bindService \u4e2d\u505a\u4e86\u4ec0\u9ebc\uff0c\u6709\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u518d\u53bb\u7814\u7a76\uff0c\u9019\u88e1\u77e5\u9053\u5b83\u8fd4\u56de\u7684\u5c31\u662f mRemote \u5c0d\u8c61\u5373\u53ef\u3002<\/p>\n<p>MainActivity.java<\/p>\n<p>[java]<br \/>\nParcel data = Parcel.obtain();<br \/>\nParcel reply = Parcel.obtain();<br \/>\ndata.writeInterfaceToken(&quot;reporter&quot;);<br \/>\ndata.writeString(&quot;this is a test string.&quot;);<br \/>\ndata.writeInt(3);<br \/>\ntry {<br \/>\n    mReporterBind.transact(BinderService.REPORT_CODE, data, reply, 0);<br \/>\n} catch (RemoteException e) {<br \/>\n    e.printStackTrace();<br \/>\n}<br \/>\nreply.enforceInterface(&quot;reporter&quot;);<br \/>\nint result = reply.readInt();<br \/>\ndata.recycle();<br \/>\nreply.recycle();<br \/>\n[\/java]<\/p>\n<p>\u901a\u904e Parcel.obtain() \u7372\u53d6\u767c\u9001\u5305\u5c0d\u8c61\u3001\u61c9\u7b54\u5305\u5c0d\u8c61\uff0c\u5beb\u5165\u6578\u64da\uff0c\u8abf\u7528 IBinder \u7684 transact \u63a5\u53e3\uff0c\u5373 mRemote.transact() \u7684\u8abf\u7528\u3002<\/p>\n<p><strong>3.4 \u5c0f\u7d50<\/strong><\/p>\n<ul class=\" list-paddingleft-2\">\n<li>\u4e00\u5207\u8907\u96dc\u7684\u908f\u8f2f\u5747\u5df2\u7d93\u88ab\u5c01\u88dd\u5728\u5be6\u73fe\u4e86 IBinder \u63a5\u53e3\u7684 Binder \u985e\u4e2d<\/li>\n<li>Activity \u4e2d\u901a\u904e bindService \u62ff\u5230 Binder Driver \u4e2d\u7684 mRemote \u5c0d\u8c61\uff08IBinder \u7684\u5be6\u4f8b\uff09\uff0c\u7136\u5f8c\u300c\u7d44\u5305\u300d\uff0c\u7136\u5f8c\u300c\u8abf\u7528 transact \u63a5\u53e3\u300d\u6309\u5e8f\u767c\u9001\u6578\u64da\u5305<\/li>\n<li>Service \u4e2d\u7e7c\u627f Binder \u985e\uff0c\u300c\u91cd\u8f09 onTransact \u51fd\u6578\u300d\uff0c\u5be6\u73fe\u53c3\u6578\u7684\u300c\u89e3\u5305\u300d\uff0c\u767c\u9001\u8fd4\u56de\u5305\u7b49\uff0c\u5728 onBind \u4e2d\u8fd4\u56de\u5177\u9ad4\u7684\u5be6\u73fe\u985e \u5982\u4e0a\u6587\u4e2d\u7684 Reporter<\/li>\n<\/ul>\n<p>\u7e3d\u7684\u8aaa\u4f86\u5c31\u662f Client \u7d44\u5305\uff0c\u8abf\u7528 transact \u767c\u9001\u6578\u64da\uff0cServer \u63a5\u5230\u8abf\u7528\uff0c\u89e3\u5305\uff0c\u8fd4\u56de\uff0c\u4e0b\u9762\u4f7f\u7528 AIDL \u7684\u6d41\u7a0b\u672c\u8cea\u4e5f\u662f\u4e00\u6a23\u3002<\/p>\n<p><strong>\u4f7f\u7528 AIDL \u5be6\u73fe Binder IPC<\/strong><\/p>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u6211\u5011\u770b\u5230\uff0c\u5b9a\u7fa9\u4e86 IReporter \u63a5\u53e3\uff0c\u4f46\u662f\u5176\u5be6 Client \u4e2d\u4e26\u6c92\u6709\u7528\u5230\uff0c\u56e0\u70ba\u6578\u64da\u7684\u7d44\u5305\u548c\u89e3\u5305\u5176\u5be6\u662f\u624b\u52d5\u7de8\u78bc\u7684\uff0c\u4e26\u4e0d\u80fd\u76f4\u63a5\u8abf\u7528\u63a5\u53e3\uff0c\u6240\u4ee5\u5176\u5be6\u5b9a\u7fa9\u63a5\u53e3\u7684\u610f\u7fa9\u7b49\u65bc 0\uff0c\u57fa\u65bc\u6b64\uff0cAndroid \u7d66\u4e86\u6211\u5011\u66f4\u597d\u7528\u7684\u65b9\u5f0f\u90a3\u5c31\u662f AIDL\uff0c\u5b9a\u7fa9\u5982\u4e0b<\/p>\n<p>IReporter.aidl<\/p>\n<p>[java]<br \/>\npackage com.android.binder;<\/p>\n<p>interface IReporter {<br \/>\n\tint report(String values, int type);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>4.1\u00a0Server<\/strong><\/p>\n<p>AidlService.java<\/p>\n<p>[java]<br \/>\npublic class AidlService extends Service {<\/p>\n<p>\tpublic static final class Reporter extends IReporter.Stub {<br \/>\n\t\t@Override<br \/>\n\t\tpublic int report(String values, int type) throws RemoteException {<br \/>\n\t\t\treturn type;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tprivate Reporter mReporter;<\/p>\n<p>\tpublic AidlService() {<br \/>\n\t\tmReporter = new Reporter();<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic IBinder onBind(Intent intent) {<br \/>\n\t\treturn mReporter;<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u9019\u88e1\u8207\u624b\u52d5\u5be6\u73fe\u7684\u65b9\u5f0f\u4e0d\u540c\u7684\u662f\uff0c\u4e00\u500b\u7e7c\u627f\u4e86 Binder\uff0c\u4e00\u500b\u7e7c\u627f\u4e86 AIDL \u81ea\u52d5\u751f\u6210\u7684 Stub \u5c0d\u8c61\uff0c\u5b83\u662f\u4ec0\u9ebc\u5462\uff1f\u6211\u5011\u53ef\u4ee5\u770b\u4e0b\u5b83\u7684\u5b9a\u7fa9<\/p>\n<p>IReporter.java<\/p>\n<p>[java]<br \/>\npublic interface IReporter extends android.os.IInterface<br \/>\n{<br \/>\n\tpublic static abstract class Stub extends android.os.Binder implements com.android.binder.IReporter {<\/p>\n<p>\t&#8230;<\/p>\n<p>\t\t@Override<br \/>\n\t\tpublic boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException<br \/>\n\t\t{<br \/>\n\t\t\tswitch (code)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tcase INTERFACE_TRANSACTION:<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\t\treply.writeString(DESCRIPTOR);<br \/>\n\t\t\t\t\treturn true;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t\tcase TRANSACTION_report:<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\t\tdata.enforceInterface(DESCRIPTOR);<br \/>\n\t\t\t\t\tjava.lang.String _arg0;<br \/>\n\t\t\t\t\t_arg0 = data.readString();<br \/>\n\t\t\t\t\tint _arg1;<br \/>\n\t\t\t\t\t_arg1 = data.readInt();<br \/>\n\t\t\t\t\tint _result = this.report(_arg0, _arg1);<br \/>\n\t\t\t\t\treply.writeNoException();<br \/>\n\t\t\t\t\treply.writeInt(_result);<br \/>\n\t\t\t\t\treturn true;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t\treturn super.onTransact(code, data, reply, flags);<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\t&#8230;<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>\u5176\u5be6\u548c\u6211\u5011\u4e0a\u6587\u7684\u5beb\u6cd5\u662f\u4e00\u6a23\u7684\uff0c\u81ea\u52d5\u751f\u6210\u7684 IReporter \u985e\u81ea\u52d5\u7d66\u6211\u5011\u8655\u7406\u4e86\u4e00\u4e9b\u53c3\u6578\u7684\u7d44\u5305\u548c\u89e3\u5305\u800c\u5df2\uff0c\u5728 case \u8a9e\u53e5\u4e2d\u8abf\u7528\u4e86 this.report \u5373\u53ef\u8abf\u7528\u5230\u81ea\u5df1\u7684\u696d\u52d9\u908f\u8f2f\u90e8\u5206\u4e86\u3002<\/p>\n<p><strong>4.2\u00a0Driver<\/strong><\/p>\n<p>\u8207\u4e0a\u6587\u4e00\u81f4\uff0c\u9084\u662f Binder \u7684\u5167\u90e8\u5c01\u88dd\u3002<\/p>\n<p><strong>4.3 Client<\/strong><\/p>\n<p>MainActivity.java<\/p>\n<p>[java]<br \/>\nprivate IReporter mReporterAidl;<br \/>\nprivate class AidlConnection implements ServiceConnection {<\/p>\n<p>\t@Override<br \/>\n\tpublic void onServiceConnected(ComponentName name, IBinder service) {<br \/>\n\t\tmReporterAidl = IReporter.Stub.asInterface(service);<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic void onServiceDisconnected(ComponentName name) {<br \/>\n\t\tmReporterAidl = null;<br \/>\n\t}<br \/>\n}<br \/>\n&#8230;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\n\t&#8230;<br \/>\n\tIntent intent = new Intent(this, AidlService.class);<br \/>\n\tbindService(intent, new AidlConnection(), BIND_AUTO_CREATE);<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>\u9019\u88e1\u8207\u624b\u52d5\u5be6\u73fe\u65b9\u5f0f\u4e5f\u6709\u5340\u5225\uff0c\u5373\u8abf\u7528\u4e86 Stub \u5c0d\u8c61\u7684 asInterface\uff0c\u5177\u9ad4\u505a\u4e86\u4ec0\u9ebc\u5462\uff1f<\/p>\n<p>[java]<br \/>\npublic static com.android.binder.IReporter asInterface(android.os.IBinder obj)<br \/>\n{<br \/>\n\tif ((obj==null)) {<br \/>\n\t\treturn null;<br \/>\n\t}<\/p>\n<p>\tandroid.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);<br \/>\n\tif (((iin!=null)&amp;&amp;(iin instanceof com.android.binder.IReporter))) {<br \/>\n\t\treturn ((com.android.binder.IReporter)iin);<br \/>\n\t}<\/p>\n<p>\treturn new com.android.binder.IReporter.Stub.Proxy(obj);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u5148\u67e5\u627e\u672c\u5730\u63a5\u53e3\u662f\u5426\u5b58\u5728\uff0c\u5224\u65b7\u662f\u5426\u662f\u672c\u5730\u8abf\u7528\uff0c\u5982\u679c\u662f\u5247\u76f4\u63a5\u8fd4\u56de IReporter \u7684\u5c0d\u8c61\uff0c\u5426\u5247\u8fd4\u56de Stub.Proxy \u5c0d\u8c61\uff0c\u9019\u500b Proxy \u5c0d\u8c61\u662f\u505a\u4ec0\u9ebc\u7684\u5462\uff1f<\/p>\n<p>[java]<br \/>\nprivate static class Proxy implements com.android.binder.IReporter<br \/>\n{<br \/>\n\tprivate android.os.IBinder mRemote;<br \/>\n\tProxy(android.os.IBinder remote)<br \/>\n\t{<br \/>\n\t\tmRemote = remote;<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic android.os.IBinder asBinder()<br \/>\n\t{<br \/>\n\t\treturn mRemote;<br \/>\n\t}<\/p>\n<p>\tpublic java.lang.String getInterfaceDescriptor()<br \/>\n\t{<br \/>\n\t\treturn DESCRIPTOR;<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic int report(java.lang.String values, int type) throws android.os.RemoteException<br \/>\n\t{<br \/>\n\t\tandroid.os.Parcel _data = android.os.Parcel.obtain();<br \/>\n\t\tandroid.os.Parcel _reply = android.os.Parcel.obtain();<br \/>\n\t\tint _result;<\/p>\n<p>\t\ttry {<br \/>\n\t\t\t_data.writeInterfaceToken(DESCRIPTOR);<br \/>\n\t\t\t_data.writeString(values);<br \/>\n\t\t\t_data.writeInt(type);<br \/>\n\t\t\tmRemote.transact(Stub.TRANSACTION_report, _data, _reply, 0);<br \/>\n\t\t\t_reply.readException();<br \/>\n\t\t\t_result = _reply.readInt();<br \/>\n\t\t}<\/p>\n<p>\t\tfinally {<br \/>\n\t\t\t_reply.recycle();<br \/>\n\t\t\t_data.recycle();<br \/>\n\t\t}<br \/>\n\t\treturn _result;<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u57fa\u672c\u4e0a\u5df2\u7d93\u5f88\u660e\u4e86\u4e86\uff0c\u5c31\u662f\u4e00\u500b\u4ee3\u7406\u5c0d\u8c61\uff0c\u5c0d\u8abf\u7528\u63a5\u53e3\u53c3\u6578\u505a\u7d44\u5305\u800c\u5df2\uff0c\u7136\u5f8c\u8abf\u7528\u4e86 mRemote.transact \u63a5\u53e3\uff0c\u548c\u4e0a\u6587\u624b\u52d5\u5be6\u73fe\u7684\u65b9\u5f0f\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<p><strong>4.4\u00a0\u5c0f\u7d50<\/strong><\/p>\n<ul class=\" list-paddingleft-2\">\n<li>AIDL \u81ea\u52d5\u751f\u6210\u4e86 Stub \u985e<\/li>\n<li>\u5728 Service \u7aef\u7e7c\u627f Stub \u985e\uff0cStub \u985e\u4e2d\u5be6\u73fe\u4e86 onTransact \u65b9\u6cd5\u5be6\u73fe\u4e86\u300c\u89e3\u5305\u300d\u7684\u529f\u80fd<\/li>\n<li>\u5728 Client \u7aef\u4f7f\u7528 Stub \u985e\u7684 Proxy \u5c0d\u8c61\uff0c\u8a72\u5c0d\u8c61\u5be6\u73fe\u4e86\u300c\u7d44\u5305\u300d\u4e26\u4e14\u8abf\u7528 transact \u7684\u529f\u80fd<\/li>\n<\/ul>\n<p>\u6709\u4e86 AIDL \u4e4b\u5f8c\uff0cIReporter \u63a5\u53e3\u5c31\u8b8a\u5f97\u6709\u610f\u7fa9\u4e86\uff0cClient \u8abf\u7528\u63a5\u53e3\uff0cServer \u7aef\u5be6\u73fe\u63a5\u53e3\uff0c\u4e00\u5207\u300c\u7d44\u5305\u300d\u3001\u300c\u89e3\u5305\u300d\u7684\u908f\u8f2f\u5c01\u88dd\u5728\u4e86 Stub \u985e\u4e2d\uff0c\u4e00\u5207\u5c31\u662f\u90a3\u9ebc\u5b8c\u7f8e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u65bc\u4e00\u76f4\u641e\u4e0d\u61c2Android Binder\uff0c\u53ea\u77e5\u9053\u662f\u4e00\u500bIPC\u6a5f\u5236\u3002\u7db2\u8def\u4e0a\u770b\u4e86\u5f88\u591a\u7bc7\uff0c\u4f46\u5230\u6700\u5f8c\u90fd\u4f3c\u61c2\u975e\u61c2\uff0c\u7136\u5f8c<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=2964\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2964","post","type-post","status-publish","format-standard","hentry","category-12"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2964"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2964\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}