{"id":6636,"date":"2024-11-30T09:12:49","date_gmt":"2024-11-30T01:12:49","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6636"},"modified":"2024-11-30T09:12:49","modified_gmt":"2024-11-30T01:12:49","slug":"android%e5%ba%94%e7%94%a8%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6636","title":{"rendered":"[Android]\u5e94\u7528\u591a\u8fdb\u7a0b\u5b9e\u73b0"},"content":{"rendered":"<p>\u5728 Android \u5e94\u7528\u4e2d\uff0c\u591a\u8fdb\u7a0b\u901a\u5e38\u662f\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\uff1a<\/p>\n<h3>1. <strong>\u4f7f\u7528\u591a\u4e2a <code>Service<\/code><\/strong><\/h3>\n<ul>\n<li>Android \u5e94\u7528\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e2d\u8fd0\u884c\u591a\u4e2a <code>Service<\/code>\u3002\u901a\u8fc7\u5728 <code>AndroidManifest.xml<\/code> \u6587\u4ef6\u4e2d\u8bbe\u7f6e <code>android:process<\/code> \u5c5e\u6027\uff0c\u53ef\u4ee5\u6307\u5b9a\u67d0\u4e2a\u670d\u52a1\u8fd0\u884c\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u8fdb\u7a0b\u4e2d\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-xml\"> <service android:name=\".MyService\"\n          android:process=\":remote\" \/><\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>MyService<\/code> \u4f1a\u5728\u4e00\u4e2a\u540d\u4e3a <code>:remote<\/code> \u7684\u72ec\u7acb\u8fdb\u7a0b\u4e2d\u8fd0\u884c\u3002\u6ce8\u610f\uff0c\u8fdb\u7a0b\u540d\u79f0\u7684\u683c\u5f0f\u901a\u5e38\u662f <code>:remote<\/code> \u8fd9\u6837\u7684\u5192\u53f7\u5f00\u5934\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u5e94\u7528\u5185\u90e8\u7684\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2. <strong>\u4f7f\u7528 <code>AIDL<\/code>\uff08Android Interface Definition Language\uff09<\/strong><\/h3>\n<ul>\n<li>Android \u63d0\u4f9b\u4e86 AIDL\uff0c\u7528\u4e8e\u5728\u4e0d\u540c\u8fdb\u7a0b\u95f4\u8fdb\u884c\u901a\u4fe1\u3002\u901a\u8fc7\u5b9a\u4e49 AIDL \u63a5\u53e3\uff0c\u4e24\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u8fdb\u884c\u8fdc\u7a0b\u65b9\u6cd5\u8c03\u7528\uff08RPC\uff09\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u4e0d\u540c\u8fdb\u7a0b\u7684 <code>Service<\/code> \u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u3002<br \/>\n\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a AIDL \u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"language-java\"> interface IRemoteService {\n     int add(int a, int b);\n }<\/code><\/pre>\n<p>\u7136\u540e\u5728\u670d\u52a1\u7aef\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\uff0c\u5e76\u901a\u8fc7 <code>Service<\/code> \u4e0e\u5ba2\u6237\u7aef\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>3. <strong>\u901a\u8fc7 <code>ContentProvider<\/code><\/strong><\/h3>\n<ul>\n<li><code>ContentProvider<\/code> \u662f Android \u63d0\u4f9b\u7684\u4e00\u79cd\u8de8\u8fdb\u7a0b\u901a\u4fe1\u673a\u5236\u3002\u5b83\u5141\u8bb8\u4e0d\u540c\u8fdb\u7a0b\u7684\u5e94\u7528\u8bbf\u95ee\u5171\u4eab\u7684\u6570\u636e\u3002\u901a\u8fc7 <code>ContentResolver<\/code>\uff0c\u5e94\u7528\u53ef\u4ee5\u4ece\u5176\u4ed6\u8fdb\u7a0b\u7684 <code>ContentProvider<\/code> \u83b7\u53d6\u6570\u636e\uff0c\u8fdb\u884c\u6570\u636e\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3>4. <strong>\u901a\u8fc7 <code>BroadcastReceiver<\/code><\/strong><\/h3>\n<ul>\n<li>\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u95f4\u53d1\u9001\u5e7f\u64ad\u4e5f\u662f\u4e00\u79cd\u8de8\u8fdb\u7a0b\u901a\u4fe1\u7684\u65b9\u5f0f\u3002\u901a\u8fc7 <code>sendBroadcast()<\/code> \u548c <code>BroadcastReceiver<\/code>\uff0c\u4e00\u4e2a\u5e94\u7528\u7684\u8fdb\u7a0b\u53ef\u4ee5\u5c06\u6d88\u606f\u53d1\u9001\u5230\u5176\u4ed6\u8fdb\u7a0b\uff0c\u5e76\u5728\u5176\u4ed6\u8fdb\u7a0b\u4e2d\u63a5\u6536\u548c\u5904\u7406\u8fd9\u4e9b\u5e7f\u64ad\u3002<\/li>\n<\/ul>\n<h3>5. <strong>\u591a\u8fdb\u7a0b\u6a21\u578b\u4e0b\u7684\u6570\u636e\u5b58\u50a8<\/strong><\/h3>\n<ul>\n<li>\u5bf9\u4e8e\u5171\u4eab\u6570\u636e\uff0cAndroid \u5e94\u7528\u901a\u5e38\u4f7f\u7528 <code>SharedPreferences<\/code>\u3001<code>SQLite<\/code> \u6216 <code>ContentProvider<\/code> \u6765\u4fdd\u8bc1\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4e00\u81f4\u6027\u3002\u7279\u522b\u662f <code>ContentProvider<\/code>\uff0c\u5b83\u80fd\u591f\u6709\u6548\u5730\u63d0\u4f9b\u8de8\u8fdb\u7a0b\u6570\u636e\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<h3>6. <strong>\u4f7f\u7528 <code>HandlerThread<\/code> \u6216 <code>AsyncTask<\/code> \u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/strong><\/h3>\n<ul>\n<li>\u867d\u7136 <code>HandlerThread<\/code> \u672c\u8eab\u5e76\u4e0d\u662f\u591a\u8fdb\u7a0b\u673a\u5236\uff0c\u4f46\u5b83\u5141\u8bb8\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u5904\u7406\u4efb\u52a1\u3002\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u5c06\u4e00\u4e9b\u8017\u65f6\u7684\u4efb\u52a1\u653e\u5728\u540e\u53f0\u8fdb\u7a0b\u4e2d\u5904\u7406\uff0c\u7136\u540e\u901a\u8fc7 Handler \u673a\u5236\u4e0e\u4e3b\u7ebf\u7a0b\u8fdb\u884c\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<h3>7. <strong>\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u548c\u5185\u5b58\u7ba1\u7406<\/strong><\/h3>\n<ul>\n<li>\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u65e0\u6cd5\u76f4\u63a5\u5171\u4eab\u5185\u5b58\uff0c\u5b83\u4eec\u5fc5\u987b\u901a\u8fc7\u67d0\u79cd IPC \u673a\u5236\uff08\u5982 AIDL\u3001Messenger \u6216 BroadcastReceiver\uff09\u8fdb\u884c\u901a\u4fe1\u3002Android \u7684 Binder \u662f\u5e95\u5c42\u7684 IPC \u673a\u5236\uff0c\u5b83\u4f7f\u5f97\u8fdb\u7a0b\u95f4\u901a\u4fe1\u53d8\u5f97\u53ef\u80fd\u3002<\/li>\n<\/ul>\n<h3>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n<ul>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u591a\u8fdb\u7a0b\u4f1a\u589e\u52a0\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u590d\u6742\u6027\u548c\u5f00\u9500\uff0c\u56e0\u6b64\uff0c\u53ea\u6709\u5728\u786e\u5b9e\u9700\u8981\u7684\u65f6\u5019\u624d\u5efa\u8bae\u4f7f\u7528\u3002<\/li>\n<li><strong>\u8de8\u8fdb\u7a0b\u7684\u6570\u636e\u5171\u4eab<\/strong>\uff1a\u8de8\u8fdb\u7a0b\u7684\u6570\u636e\u5171\u4eab\u9700\u8981\u8c28\u614e\u8bbe\u8ba1\uff0c\u907f\u514d\u51fa\u73b0\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u3002<\/li>\n<li><strong>\u751f\u547d\u5468\u671f\u7ba1\u7406<\/strong>\uff1a\u4e0d\u540c\u8fdb\u7a0b\u4e2d\u7684\u7ec4\u4ef6\u751f\u547d\u5468\u671f\u4e0d\u540c\uff0c\u9700\u8981\u7279\u522b\u5173\u6ce8\u670d\u52a1\u7684\u542f\u52a8\u3001\u505c\u6b62\u548c\u7ed1\u5b9a\u7b49\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u603b\u7ed3\u6765\u8bf4\uff0cAndroid \u7684\u591a\u8fdb\u7a0b\u673a\u5236\u4e3b\u8981\u4f9d\u9760 <code>Service<\/code>\u3001<code>AIDL<\/code>\u3001<code>ContentProvider<\/code> \u548c\u5e7f\u64ad\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u8de8\u8fdb\u7a0b\u7684\u901a\u4fe1\u4e0e\u534f\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Android \u5e94\u7528\u4e2d\uff0c\u591a\u8fdb\u7a0b\u901a\u5e38\u662f\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\uff1a 1. \u4f7f\u7528\u591a\u4e2a Service Androi&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6636\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-6636","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6636"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6636\/revisions"}],"predecessor-version":[{"id":6637,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6636\/revisions\/6637"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}