{"id":9,"date":"2025-09-04T21:59:19","date_gmt":"2025-09-04T13:59:19","guid":{"rendered":"http:\/\/www.yun-games.com\/?p=9"},"modified":"2025-09-04T21:59:22","modified_gmt":"2025-09-04T13:59:22","slug":"intent%e4%b8%ad%e7%9a%84setdata%e7%94%a8%e5%a4%84%e4%b8%8e%e6%b8%85%e5%8d%95%e9%87%8c%e7%9a%84data%e6%a0%87%e7%ad%be%e6%98%af%e5%b9%b2%e5%98%9b%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.yun-games.com\/?p=9","title":{"rendered":"intent\u4e2d\u7684setData\u7528\u5904\u4e0e\u6e05\u5355\u91cc\u7684data\u6807\u7b7e<data android:scheme=\"...\">\u662f\u5e72\u561b\u7684<\/data>"},"content":{"rendered":"\n<p>intent\u4e2d\u7684setData\u7528\u5904\uff0c\u8fd8\u6709\u6559\u7a0b\u597d\u50cf\u5728\u89c6\u9891\u4e2d\u8bb2\u5230\u4e86\u5728activity\u4e2d\u8bbe\u7f6e&lt;data android:scheme>\u8fd9\u662f\u5e72\u561bde<\/p>\n\n\n\n<p><code>setData()<\/code> \u5e72\u7684\u53ea\u6709\u4e00\u4ef6\u4e8b\uff1a<br><strong>\u628a\u4e00\u6761 Uri \u585e\u8fdb Intent\uff0c\u544a\u8bc9\u7cfb\u7edf\u201c\u6211\u8981\u64cd\u4f5c\u7684\u8fd9\u4e2a\u2018\u4e1c\u897f\u2019\u5728\u54ea\u513f\u201d\u3002<\/strong><br>\u6ca1\u6709\u5b83\uff0c\u9690\u5f0f Intent \u5c31\u53ea\u5269\u201c\u52a8\u4f5c\u201d\uff08action\uff09\u548c\u201c\u7c7b\u522b\u201d\uff08category\uff09\uff0c\u7cfb\u7edf\u65e0\u6cd5\u7cbe\u786e\u5339\u914d\u5230\u7ec4\u4ef6\uff1b\u6709\u4e86\u5b83\uff0c\u624d\u80fd\u8ddf\u6e05\u5355\u91cc\u7684 <code>&lt;data&gt;<\/code> \u6807\u7b7e\u201c\u5bf9\u6697\u53f7\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>setData()<\/code> \u5230\u5e95\u6709\u4ec0\u4e48\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val intent = Intent(Intent.ACTION_VIEW)      \/\/ \u52a8\u4f5c\uff1a\u67e5\u770b\nintent.setData(Uri.parse(\"https:\/\/www.example.com\"))  \/\/ \u6570\u636e\uff1a\u5177\u4f53\u770b\u54ea\u4e2a\nstartActivity(intent)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7cfb\u7edf\u4f1a\u628a <code>action=VIEW<\/code> + <code>data=https:\/\/www.example.com<\/code> \u62ff\u53bb\u8ddf\u6240\u6709 <strong>exported=true<\/strong> \u4e14\u58f0\u660e\u4e86 <code>&lt;intent-filter><\/code> \u7684 Activity \u5339\u914d\u3002<\/li>\n\n\n\n<li>\u53ea\u6709\u90a3\u4e9b\u8fc7\u6ee4\u5668\u91cc <strong>scheme\u3001host\u3001path \u90fd\u543b\u5408<\/strong> \u7684 Activity \u624d\u4f1a\u88ab\u5f39\u51fa\u6765\uff08\u6216\u88ab\u4f60\u542f\u52a8\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6e05\u5355\u91cc\u7684 <code>&lt;data android:scheme=\"...\"&gt;<\/code> \u662f\u5e72\u561b\u7684<\/h3>\n\n\n\n<p>\u5b83\u5c31\u662f\u5728 <strong>\u58f0\u660e\u81ea\u5df1\u613f\u610f\u63a5\u53d7\u4ec0\u4e48\u6837\u7684 Uri<\/strong>\u3002<br>\u6ca1\u6709 <code>&lt;data&gt;<\/code> \u7684\u8fc7\u6ee4\u5668\u7b49\u4e8e\u201c\u6211\u4ec0\u4e48 Uri \u90fd\u4e0d\u63a5\u201d\uff0c\u5e26 <code>setData()<\/code> \u7684 Intent \u6839\u672c\u5339\u914d\u4e0d\u5230\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;activity android:name=\".DeepLinkActivity\" android:exported=\"true\"&gt;\n    &lt;intent-filter&gt;\n        &lt;action android:name=\"android.intent.action.VIEW\"\/&gt;\n        &lt;category android:name=\"android.intent.category.DEFAULT\"\/&gt;\n        &lt;category android:name=\"android.intent.category.BROWSABLE\"\/&gt;\n        &lt;!-- \u53ea\u63a5 https \u4e14 host \u662f example.com \u7684 Uri --&gt;\n        &lt;data android:scheme=\"https\"\n              android:host=\"example.com\"\n              android:pathPattern=\"\/item\/.*\"\/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/activity&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5916\u90e8\u6d4f\u89c8\u5668\u70b9\u51fb <code>https:\/\/example.com\/item\/123<\/code> \u5c31\u4f1a\u5524\u8d77\u4f60\u7684 <code>DeepLinkActivity<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c scheme \u5199\u6210 <code>myapp<\/code>\uff0c\u90a3\u53ea\u6709 <code>myapp:\/\/detail\/123<\/code> \u624d\u80fd\u5524\u8d77\uff0c\u6d4f\u89c8\u5668\u518d\u4e5f\u8fdb\u4e0d\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5e38\u89c1 scheme \u5bf9\u7167\u8868<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>scheme<\/th><th>\u5178\u578b\u7528\u9014<\/th><\/tr><\/thead><tbody><tr><td><code>http<\/code> \/ <code>https<\/code><\/td><td>\u7f51\u9875\u3001DeepLink<\/td><\/tr><tr><td><code>file<\/code><\/td><td>\u672c\u5730\u6587\u4ef6\uff08Android 7+ \u9700 FileProvider\uff09<\/td><\/tr><tr><td><code>content<\/code><\/td><td>\u901a\u8fc7 ContentProvider \u66b4\u9732\u7684 URI\uff08\u6700\u5b89\u5168\uff09<\/td><\/tr><tr><td><code>market<\/code><\/td><td>\u8df3 Google Play<\/td><\/tr><tr><td><code>myapp<\/code><\/td><td>\u81ea\u5b9a\u4e49 DeepLink \/ \u591a\u8fdb\u7a0b\u901a\u4fe1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. setData() \u4e0e setType() \u540c\u65f6\u7528\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u80fd<\/strong>\u76f4\u63a5 <code>setData()<\/code> \u540e\u53c8 <code>setType()<\/code>\uff0c\u540e\u8005\u4f1a\u6e05\u6389\u524d\u8005\u3002<\/li>\n\n\n\n<li>\u8981\u540c\u65f6\u6307\u5b9a Uri \u548c MIME\uff0c\u7528 <strong>\u5408\u4f53 API<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>intent.setDataAndType(uri, \"image\/jpeg\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4e00\u53e5\u8bdd\u603b\u7ed3<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>setData() \u7ed9 Intent \u585e Uri\uff1b\u6e05\u5355\u91cc\u7684 <code>&lt;data&gt;<\/code> \u58f0\u660e\u7ec4\u4ef6\u80fd\u63a5\u4ec0\u4e48 Uri\u3002<\/strong><br><strong>\u4e24\u8005\u5408\u8d77\u6765\u624d\u5b8c\u6210\u201c\u52a8\u4f5c + \u6570\u636e\u201d\u7684\u7cbe\u786e\u5339\u914d\uff0c\u662f\u9690\u5f0f Intent \u7684\u57fa\u77f3\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u90a3\u4e48uri\u662f\u4f1a\u4ea4\u7ed9\u6d4f\u89c8\u5668\u5904\u7406\u8fd8\u662factivity\u4e2d\u5904\u7406<br>\u6bd4\u5982\u6211\u4f20\u5165\u4e86\u4e00\u4e2a\u94fe\u63a5\uff0c\u662f\u4f1a\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u7f51\u9875\u8fd8\u662f\u8be5activity\u4e2d\u6253\u5f00<\/p>\n\n\n\n<p>\u770b <strong>\u8c01\u62a2\u5148 + \u8c01\u5339\u914d<\/strong>\u2014\u2014Uri \u4e0d\u4f1a\u201c\u81ea\u52a8\u201d\u8fdb\u6d4f\u89c8\u5668\uff0c\u800c\u662f\u7cfb\u7edf\u628a <strong>\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684 Activity \u62c9\u4e2a\u5217\u8868<\/strong>\uff08\u4e5f\u53eb <strong>disambiguation dialog<\/strong>\uff09\uff0c\u8ba9\u7528\u6237\u6311\uff1b\u5982\u679c\u53ea\u6709 <strong>\u4e00\u4e2a<\/strong> \u5339\u914d\uff0c\u5c31\u76f4\u63a5\u8fdb\u90a3\u4e00\u4e2a\uff0c<strong>\u6d4f\u89c8\u5668\u8fd8\u662f\u4f60\u81ea\u5df1\u7684 Activity \u90fd\u6709\u53ef\u80fd<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5339\u914d\u89c4\u5219\uff08\u7b80\u5316\u7248\uff09<\/h3>\n\n\n\n<p>\u7cfb\u7edf\u6309 <strong>\u4f18\u5148\u7ea7\u987a\u5e8f<\/strong> \u8fc7\u6ee4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>scheme<\/strong><\/li>\n\n\n\n<li><strong>host<\/strong>\uff08\u4ec5\u5f53 scheme \u662f http\/https \u4e14\u58f0\u660e\u4e86 host\uff09<\/li>\n\n\n\n<li><strong>path\/pathPrefix\/pathPattern<\/strong><\/li>\n\n\n\n<li><strong>mimeType<\/strong>\uff08\u82e5\u6709 <code>setType()<\/code>\uff09<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u573a\u666f\u5b9e\u6d4b<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f60\u4f20\u7684 Uri<\/th><th>\u8bbe\u5907\u5df2\u5b89\u88c5<\/th><th>\u7ed3\u679c<\/th><\/tr><\/thead><tbody><tr><td><code>https:\/\/example.com\/item\/123<\/code><\/td><td><strong>\u53ea\u6709\u6d4f\u89c8\u5668<\/strong> \u58f0\u660e\u4e86 <code>https<\/code><\/td><td>\u76f4\u63a5\u8fdb <strong>\u6d4f\u89c8\u5668<\/strong><\/td><\/tr><tr><td>\u540c\u4e0a<\/td><td><strong>\u6d4f\u89c8\u5668 + \u4f60\u7684 Activity<\/strong> \u90fd\u58f0\u660e <code>https<\/code> + <code>host=example.com<\/code><\/td><td>\u5f39\u51fa <strong>\u9009\u62e9\u6846<\/strong>\uff08\u542b\u6d4f\u89c8\u5668\u548c\u4f60\uff09<\/td><\/tr><tr><td>\u540c\u4e0a<\/td><td><strong>\u53ea\u6709\u4f60\u7684 Activity<\/strong> \u58f0\u660e\u4e86 <code>https<\/code> + <code>host=example.com<\/code><\/td><td>\u76f4\u63a5\u8fdb <strong>\u4f60\u7684 Activity<\/strong>\uff0c\u6d4f\u89c8\u5668 <strong>\u4e0d\u4f1a<\/strong>\u51fa\u73b0<\/td><\/tr><tr><td><code>myapp:\/\/detail\/123<\/code><\/td><td>\u53ea\u6709\u4f60\u7684 Activity \u58f0\u660e <code>myapp<\/code> \u65b9\u6848<\/td><td>\u76f4\u63a5\u8fdb <strong>\u4f60\u7684 Activity<\/strong>\uff1b\u6d4f\u89c8\u5668\u6839\u672c\u6536\u4e0d\u5230<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5982\u4f55\u786e\u4fdd <strong>\u4e00\u5b9a\u8fdb\u81ea\u5df1\u7684 Activity<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7528 <strong>\u81ea\u5b9a\u4e49 scheme<\/strong>\uff08\u4e0d\u4e0e\u6d4f\u89c8\u5668\u51b2\u7a81\uff09<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;data android:scheme=\"myapp\" \/&gt;<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5f3a\u5236\u6307\u5b9a\u5305\u540d<\/strong>\uff0c\u7ed5\u8fc7\u9009\u62e9\u6846\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   val intent = Intent(Intent.ACTION_VIEW,\n                       Uri.parse(\"myapp:\/\/detail\/123\"))\n   intent.setPackage(\"com.example.yourapp\")  \/\/ \u76f4\u63a5\u9501\u5b9a\u81ea\u5df1\n   startActivity(intent)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u5229\u7528 Android 12 \u7684 App-link<\/strong>\uff08\u9700\u6570\u5b57\u8bc1\u4e66 + \u670d\u52a1\u5668\u9a8c\u8bc1\uff09<br>\u58f0\u660e <code>android:autoVerify=\"true\"<\/code> \u4e14\u4e0a\u4f20 <code>assetlinks.json<\/code>\uff0c\u53ef\u628a <code>https<\/code> \u94fe\u63a5 <strong>\u9ed8\u8ba4<\/strong> \u5bfc\u5411\u4f60\u7684 Activity\uff0c<strong>\u4e0d\u51fa\u73b0\u9009\u62e9\u6846<\/strong>\uff08\u7528\u6237\u53ef\u624b\u52a8\u6539\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4e00\u53e5\u8bdd\u7ed3\u8bba<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Uri \u4ea4\u7ed9\u8c01\uff0c\u4e0d\u7531\u201c\u6d4f\u89c8\u5668\u201d\u8bf4\u4e86\u7b97\uff0c\u800c\u7531\u201c\u8c01\u7684 <code>&lt;intent-filter&gt;<\/code> \u5339\u914d + \u7528\u6237\/\u7cfb\u7edf\u9009\u62e9\u201d\u51b3\u5b9a\uff1b<\/strong><br><strong>\u60f3\u72ec\u5360\u94fe\u63a5\uff0c\u5c31\u7528\u81ea\u5b9a\u4e49 scheme \u6216 verified app-link\uff0c\u518d\u6216\u8005 setPackage \u76f4\u63a5\u70b9\u540d\u3002<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>intent\u4e2d\u7684setData\u7528\u5904\uff0c\u8fd8\u6709\u6559\u7a0b\u597d\u50cf\u5728\u89c6\u9891\u4e2d\u8bb2\u5230\u4e86\u5728activity\u4e2d\u8bbe\u7f6e&lt;data and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.yun-games.com\/index.php?rest_route=\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yun-games.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yun-games.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yun-games.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yun-games.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9"}],"version-history":[{"count":1,"href":"https:\/\/www.yun-games.com\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":10,"href":"https:\/\/www.yun-games.com\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions\/10"}],"wp:attachment":[{"href":"https:\/\/www.yun-games.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yun-games.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yun-games.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}