{"id":58564,"date":"2024-10-12T20:50:18","date_gmt":"2024-10-12T12:50:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58564\/"},"modified":"2024-10-12T20:50:18","modified_gmt":"2024-10-12T12:50:18","slug":"android%e6%87%89%e7%94%a8%e7%9a%84uri%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5%e5%a4%9a%e6%a2%9d%e6%95%b8%e6%93%9a%e5%ba%ab-android-uri%e5%a4%9a%e6%a2%9d%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58564\/","title":{"rendered":"Android\u61c9\u7528\u7684URI\u5982\u4f55\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab (android uri\u591a\u689d\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"android%e6%87%89%e7%94%a8%e7%9a%84uri%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5%e5%a4%9a%e6%a2%9d%e6%95%b8%e6%93%9a%e5%ba%ab-YXoDqbQtey\">Android\u61c9\u7528\u7684URI\u5982\u4f55\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab<\/h1>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0cURI\uff08\u7d71\u4e00\u8cc7\u6e90\u6a19\u8b58\u7b26\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u6578\u64da\u5eab\u6642\u3002\u7576\u61c9\u7528\u9700\u8981\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab\u6642\uff0c\u6b63\u78ba\u4f7f\u7528URI\u53ef\u4ee5\u7c21\u5316\u6578\u64da\u7684\u5b58\u53d6\u548c\u7ba1\u7406\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Android\u61c9\u7528\u4e2d\u4f7f\u7528URI\u4f86\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afuri%ef%bc%9f-YXoDqbQtey\">\u4ec0\u9ebc\u662fURI\uff1f<\/h2>\n<p>URI\u662f\u4e00\u7a2e\u7528\u65bc\u6a19\u8b58\u8cc7\u6e90\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u7528\u65bc\u5b9a\u4f4d\u7db2\u7d61\u8cc7\u6e90\u6216\u672c\u5730\u8cc7\u6e90\u3002\u5728Android\u4e2d\uff0cURI\u5e38\u7528\u65bc\u5167\u5bb9\u63d0\u4f9b\u8005\uff08Content Provider\uff09\uff0c\u9019\u662f\u4e00\u7a2e\u7528\u65bc\u5728\u4e0d\u540c\u61c9\u7528\u4e4b\u9593\u5171\u4eab\u6578\u64da\u7684\u6a5f\u5236\u3002\u901a\u904eURI\uff0c\u61c9\u7528\u53ef\u4ee5\u8a2a\u554f\u548c\u64cd\u4f5c\u5176\u4ed6\u61c9\u7528\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e9%80%a3%e6%8e%a5%e5%a4%9a%e6%a2%9d%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-YXoDqbQtey\">\u70ba\u4ec0\u9ebc\u9700\u8981\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab\uff1f<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u5f9e\u591a\u500b\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u3002\u4f8b\u5982\uff0c\u4e00\u500b\u793e\u4ea4\u5a92\u9ad4\u61c9\u7528\u53ef\u80fd\u9700\u8981\u5f9e\u7528\u6236\u6578\u64da\u5eab\u548c\u6d88\u606f\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4fe1\u606f\u3002\u9019\u6a23\u7684\u9700\u6c42\u4fc3\u4f7f\u958b\u767c\u8005\u5c0b\u627e\u6709\u6548\u7684\u65b9\u5f0f\u4f86\u7ba1\u7406\u548c\u8a2a\u554f\u591a\u689d\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8uri%e9%80%a3%e6%8e%a5%e5%a4%9a%e6%a2%9d%e6%95%b8%e6%93%9a%e5%ba%ab-YXoDqbQtey\">\u5982\u4f55\u4f7f\u7528URI\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5275\u5efa\u5167\u5bb9\u63d0\u4f9b\u8005\uff08Content Provider\uff09\u4ee5\u7ba1\u7406\u6578\u64da\u5eab\u3002<\/li>\n<li>\u70ba\u6bcf\u500b\u6578\u64da\u5eab\u5b9a\u7fa9\u4e0d\u540c\u7684URI\u3002<\/li>\n<li>\u4f7f\u7528\u9019\u4e9bURI\u4f86\u57f7\u884c\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%80%ef%bc%9a%e5%89%b5%e5%bb%ba%e5%85%a7%e5%ae%b9%e6%8f%90%e4%be%9b%e8%80%85-YXoDqbQtey\">\u6b65\u9a5f\u4e00\uff1a\u5275\u5efa\u5167\u5bb9\u63d0\u4f9b\u8005<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u5167\u5bb9\u63d0\u4f9b\u8005\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u5167\u5bb9\u63d0\u4f9b\u8005\u793a\u4f8b\uff1a<\/p>\n<pre><code>public class MyContentProvider extends ContentProvider {\n    @Override\n    public boolean onCreate() {\n        \/\/ \u521d\u59cb\u5316\u6578\u64da\u5eab\n        return true;\n    }\n\n    @Override\n    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {\n        \/\/ \u6839\u64daURI\u67e5\u8a62\u6578\u64da\n        return null;\n    }\n\n    \/\/ \u5176\u4ed6\u5fc5\u8981\u7684\u65b9\u6cd5\uff08insert, update, delete\u7b49\uff09\n}<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e4%ba%8c%ef%bc%9a%e5%ae%9a%e7%be%a9uri-YXoDqbQtey\">\u6b65\u9a5f\u4e8c\uff1a\u5b9a\u7fa9URI<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u70ba\u6bcf\u500b\u6578\u64da\u5eab\u5b9a\u7fa9\u4e0d\u540c\u7684URI\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>public static final Uri USER_URI = Uri.parse(\"content:\/\/com.example.app\/user\");\npublic static final Uri MESSAGE_URI = Uri.parse(\"content:\/\/com.example.app\/message\");<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%89%ef%bc%9a%e5%9f%b7%e8%a1%8c%e6%93%8d%e4%bd%9c-YXoDqbQtey\">\u6b65\u9a5f\u4e09\uff1a\u57f7\u884c\u64cd\u4f5c<\/h3>\n<p>\u6700\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u9019\u4e9bURI\u4f86\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u67e5\u8a62\u7528\u6236\u6578\u64da\uff1a<\/p>\n<pre><code>Cursor cursor = getContentResolver().query(USER_URI, null, null, null, null);\nif (cursor != null) {\n    while (cursor.moveToNext()) {\n        \/\/ \u8655\u7406\u6578\u64da\n    }\n    cursor.close();\n}<\/code><\/pre>\n<h2 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e9%80%a3%e6%8e%a5%e7%94%a8%e6%88%b6%e5%92%8c%e6%b6%88%e6%81%af%e6%95%b8%e6%93%9a%e5%ba%ab-YXoDqbQtey\">\u793a\u4f8b\uff1a\u9023\u63a5\u7528\u6236\u548c\u6d88\u606f\u6578\u64da\u5eab<\/h2>\n<p>\u5047\u8a2d\u60a8\u6709\u5169\u500b\u6578\u64da\u5eab\uff1a\u7528\u6236\u6578\u64da\u5eab\u548c\u6d88\u606f\u6578\u64da\u5eab\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u540c\u6642\u67e5\u8a62\u9019\u5169\u500b\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>Cursor userCursor = getContentResolver().query(USER_URI, null, null, null, null);\nCursor messageCursor = getContentResolver().query(MESSAGE_URI, null, null, null, null);\n\nif (userCursor != null &amp;&amp; messageCursor != null) {\n    \/\/ \u8655\u7406\u7528\u6236\u6578\u64da\n    \/\/ \u8655\u7406\u6d88\u606f\u6578\u64da\n    userCursor.close();\n    messageCursor.close();\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-YXoDqbQtey\">\u7e3d\u7d50<\/h2>\n<p>\u5728Android\u61c9\u7528\u4e2d\uff0c\u4f7f\u7528URI\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab\u662f\u4e00\u500b\u6709\u6548\u7684\u6578\u64da\u7ba1\u7406\u7b56\u7565\u3002\u901a\u904e\u5275\u5efa\u5167\u5bb9\u63d0\u4f9b\u8005\u4e26\u70ba\u6bcf\u500b\u6578\u64da\u5eab\u5b9a\u7fa9\u4e0d\u540c\u7684URI\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u8a2a\u554f\u548c\u64cd\u4f5c\u591a\u500b\u6578\u64da\u6e90\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u7684\u53ef\u8a2a\u554f\u6027\uff0c\u9084\u7c21\u5316\u4e86\u6578\u64da\u7684\u7ba1\u7406\u6d41\u7a0b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u6258\u7ba1\u60a8\u7684\u6578\u64da\u5eab\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728Android\u61c9\u7528\u4e2d\u4f7f\u7528URI\u9023\u63a5\u591a\u689d\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u7684\u9ad8\u6548\u7ba1\u7406\u8207\u8a2a\u554f\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-58564","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58564","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=58564"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58564\/revisions"}],"predecessor-version":[{"id":58565,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58564\/revisions\/58565"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}