{"id":58572,"date":"2024-10-12T20:53:15","date_gmt":"2024-10-12T12:53:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58572\/"},"modified":"2024-10-12T20:53:16","modified_gmt":"2024-10-12T12:53:16","slug":"android%e6%87%89%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%8a%80%e5%b7%a7-android-%e5%a4%9a%e5%80%8b%e7%b7%9a%e7%a8%8b%e5%90%8c%e6%99%82%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58572\/","title":{"rendered":"Android\u61c9\u7528\u6578\u64da\u5eab\u591a\u7dda\u7a0b\u64cd\u4f5c\u6280\u5de7 (android \u591a\u500b\u7dda\u7a0b\u540c\u6642\u64cd\u4f5c\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"android%e6%87%89%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%8a%80%e5%b7%a7-mCnLawCDza\">Android\u61c9\u7528\u6578\u64da\u5eab\u591a\u7dda\u7a0b\u64cd\u4f5c\u6280\u5de7<\/h1>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u64cd\u4f5c\u662f\u61c9\u7528\u7a0b\u5e8f\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u8907\u96dc\u6027\u589e\u52a0\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u9700\u6c42\u4e5f\u96a8\u4e4b\u589e\u9577\u3002\u7279\u5225\u662f\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e0b\uff0c\u5982\u4f55\u5b89\u5168\u4e14\u9ad8\u6548\u5730\u64cd\u4f5c\u6578\u64da\u5eab\u6210\u70ba\u4e86\u4e00\u500b\u6311\u6230\u3002\u672c\u6587\u5c07\u63a2\u8a0eAndroid\u61c9\u7528\u4e2d\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab-mCnLawCDza\">\u70ba\u4ec0\u9ebc\u9700\u8981\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u4e3b\u7dda\u7a0b\uff08UI\u7dda\u7a0b\uff09\u8ca0\u8cac\u8655\u7406\u7528\u6236\u754c\u9762\u548c\u7528\u6236\u4ea4\u4e92\u3002\u5982\u679c\u5728\u4e3b\u7dda\u7a0b\u4e2d\u57f7\u884c\u8017\u6642\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5c07\u6703\u5c0e\u81f4\u61c9\u7528\u7a0b\u5e8f\u7684\u754c\u9762\u5361\u9813\uff0c\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u3002\u56e0\u6b64\uff0c\u5c07\u6578\u64da\u5eab\u64cd\u4f5c\u653e\u5728\u5b50\u7dda\u7a0b\u4e2d\u57f7\u884c\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u4e3b\u7dda\u7a0b\u7684\u6d41\u66a2\u6027\uff0c\u63d0\u5347\u61c9\u7528\u7684\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8asynctask%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-mCnLawCDza\">\u4f7f\u7528AsyncTask\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>AsyncTask\u662f\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u5f0f\u4f86\u5728Android\u4e2d\u57f7\u884c\u7570\u6b65\u4efb\u52d9\u3002\u5b83\u5141\u8a31\u958b\u767c\u8005\u5728\u80cc\u666f\u7dda\u7a0b\u4e2d\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u5728\u5b8c\u6210\u5f8c\u66f4\u65b0UI\u3002<\/p>\n<pre><code>public class DatabaseTask extends AsyncTask&lt;Void, Void, List&gt; {\n    @Override\n    protected List doInBackground(Void... voids) {\n        \/\/ \u5728\u9019\u88e1\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n        return database.getData();\n    }\n\n    @Override\n    protected void onPostExecute(List result) {\n        \/\/ \u66f4\u65b0UI\n        updateUI(result);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0cdoInBackground\u65b9\u6cd5\u4e2d\u57f7\u884c\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u800conPostExecute\u65b9\u6cd5\u5247\u7528\u65bc\u66f4\u65b0UI\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8handler%e5%92%8crunnable%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-mCnLawCDza\">\u4f7f\u7528Handler\u548cRunnable\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528Handler\u548cRunnable\u4f86\u8655\u7406\u591a\u7dda\u7a0b\u64cd\u4f5c\u3002\u9019\u7a2e\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u591a\u6b21\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\u3002<\/p>\n<pre><code>Handler handler = new Handler(Looper.getMainLooper());\nnew Thread(new Runnable() {\n    @Override\n    public void run() {\n        \/\/ \u5728\u9019\u88e1\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n        final List result = database.getData();\n        \n        \/\/ \u4f7f\u7528Handler\u66f4\u65b0UI\n        handler.post(new Runnable() {\n            @Override\n            public void run() {\n                updateUI(result);\n            }\n        });\n    }\n}).start();\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684\u7dda\u7a0b\u4f86\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u4f7f\u7528Handler\u5c07\u7d50\u679c\u767c\u9001\u56de\u4e3b\u7dda\u7a0b\u4ee5\u66f4\u65b0UI\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8room%e6%8c%81%e4%b9%85%e5%8c%96%e5%ba%ab-mCnLawCDza\">\u4f7f\u7528Room\u6301\u4e45\u5316\u5eab<\/h2>\n<p>Room\u662fAndroid\u5b98\u65b9\u63a8\u85a6\u7684\u6578\u64da\u5eab\u6301\u4e45\u5316\u5eab\uff0c\u5b83\u7c21\u5316\u4e86\u6578\u64da\u5eab\u64cd\u4f5c\u4e26\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u6027\u80fd\u3002Room\u652f\u6301LiveData\u548cRxJava\uff0c\u9019\u4f7f\u5f97\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\u64cd\u4f5c\u6578\u64da\u5eab\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002<\/p>\n<pre><code>@Dao\npublic interface UserDao {\n    @Insert\n    void insert(User user);\n\n    @Query(\"SELECT * FROM user\")\n    LiveData&lt;List&gt; getAllUsers();\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528Room\u6642\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728\u4e0d\u540c\u7684\u7dda\u7a0b\u4e2d\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u800c\u4e0d\u5fc5\u64d4\u5fc3\u6578\u64da\u5eab\u7684\u9396\u5b9a\u554f\u984c\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-mCnLawCDza\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fdd\u6578\u64da\u5eab\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u7684\uff0c\u4ee5\u907f\u514d\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u554f\u984c\u3002<\/li>\n<li>\u4f7f\u7528\u9069\u7576\u7684\u540c\u6b65\u6a5f\u5236\u4f86\u9632\u6b62\u591a\u500b\u7dda\u7a0b\u540c\u6642\u8a2a\u554f\u6578\u64da\u5eab\u3002<\/li>\n<li>\u8003\u616e\u4f7f\u7528\u4e8b\u52d9\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-mCnLawCDza\">\u7e3d\u7d50<\/h2>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0c\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u662f\u63d0\u5347\u61c9\u7528\u6027\u80fd\u7684\u95dc\u9375\u3002\u7121\u8ad6\u662f\u4f7f\u7528AsyncTask\u3001Handler\u9084\u662fRoom\u6301\u4e45\u5316\u5eab\uff0c\u958b\u767c\u8005\u90fd\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684Android\u61c9\u7528\u958b\u767c\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1Android\u61c9\u7528\u6578\u64da\u5eab\u591a\u7dda\u7a0b\u64cd\u4f5c\u6280\u5de7\uff0c\u63d0\u5347\u6027\u80fd\u8207\u6548\u7387\uff0c\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\uff0c\u5be6\u73fe\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-58572","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=58572"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58572\/revisions"}],"predecessor-version":[{"id":58573,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58572\/revisions\/58573"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}