{"id":1876,"date":"2023-03-30T22:34:47","date_gmt":"2023-03-30T14:34:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1876"},"modified":"2023-04-22T09:03:06","modified_gmt":"2023-04-22T01:03:06","slug":"simple-use-of-android-deeplink","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/30\/simple-use-of-android-deeplink\/","title":{"rendered":"Android DeepLink\u7684\u7b80\u5355\u4f7f\u7528"},"content":{"rendered":"<h2>Deeplink\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>Deeplink\uff0c\u7b80\u5355\u8bb2\uff0c\u5c31\u662f\u5728\u4f60\u624b\u673a\u4e0a\u70b9\u51fb\u4e00\u4e2a\u94fe\u63a5\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u94fe\u63a5\u5230app\u5185\u90e8\u7684\u67d0\u4e2a\u9875\u9762\uff0c\u800c\u4e0d\u662fapp\u6b63\u5e38\u6253\u5f00\u65f6\u663e\u793a\u7684\u9996\u9875\u3002\u7d2f\u4f3cweb\uff0c\u4e00\u4e2a\u94fe\u63a5\u5c31\u53ef\u4ee5\u76f4\u63a5\u6253\u5f00web\u7684\u7f51\u9875\uff0capp\u7684\u5185\u9875\u6253\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528deeplink\u6765\u5b9e\u73b0<\/p>\n<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<p><!-- more --><\/p>\n<p>\u5728\u6e05\u5355\u6587\u4ef6\u914d\u7f6e<\/p>\n<pre><code class=\"language-xml\">&lt;activity android:name=&quot;.MainActivity&quot;&gt;\n    &lt;intent-filter&gt;\n        &lt;action android:name=&quot;android.intent.action.MAIN&quot; \/&gt;\n        &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; \/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/activity&gt;\n&lt;activity android:name=&quot;.DeepLinkActivity&quot;&gt;\n    &lt;intent-filter&gt;\n        &lt;action android:name=&quot;android.intent.action.VIEW&quot; \/&gt;\n        &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; \/&gt;\n        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; \/&gt;\n        &lt;data\n            android:host=&quot;appblog&quot;\n            android:scheme=&quot;cn&quot;\n            \/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/activity&gt;<\/code><\/pre>\n<p>\u4e3b\u8981\u4ee3\u7801BROWSABLE\u610f\u601d\u662f\u53ef\u4ee5\u88ab\u5916\u90e8\u6d4f\u89c8\u5668\u6253\u5f00\uff0chost\u548cscheme\u7c7b\u4f3c<code>http:\/\/xxxx<\/code>\uff0c\u5728\u8df3\u8f6c\u7684\u65f6\u5019\u4f1a\u7528<\/p>\n<pre><code class=\"language-xml\">&lt;intent-filter&gt;\n    &lt;action android:name=&quot;android.intent.action.VIEW&quot; \/&gt;\n    &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; \/&gt;\n    &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; \/&gt;\n    &lt;data\n        android:host=&quot;appblog&quot;\n        android:scheme=&quot;cn&quot;\n        \/&gt;\n&lt;\/intent-filter&gt;<\/code><\/pre>\n<p>\u5199\u4e00\u4e2ahtml\u94fe\u63a5\u8fc7\u53bb<code>&lt;a href=&quot;cn:\/\/appblog\/20&quot;&gt;\u5c1d\u8bd5\u6253\u5f00deeplink\u754c\u9762&lt;\/a&gt;<\/code>\u8981\u4e0e\u4e0a\u9762data\u91cc\u9762\u7684\u5b57\u6bb5\u7684\u5bf9\u5e94\u8d77\u6765<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;Document&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;a href=&quot;cn:\/\/appblog\/joe&quot;&gt;\u5c1d\u8bd5\u6253\u5f00deeplink\u754c\u9762&lt;\/a&gt;\n\u6ce8\u610f\uff1acn\u5c31\u662f\u81ea\u5df1\u5199\u7684scheme\uff0cappblog\u662fhost\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4efb\u610f\u5b57\u7b26\u4e32\uff0c20\u662f\u6a21\u62df\u4f20\u9012\u7684\u503c\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u5728MainActivity\u5199\u4e00\u4e2awebview\u7528\u6237\u52a0\u8f7d\u521a\u5199\u7684\u7f51\u9875<\/p>\n<pre><code class=\"language-java\">public class MainActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        WebView webView = findViewById(R.id.web_view);\n        webView.loadUrl(&quot;file:\/\/\/android_asset\/&quot; + &quot;deeplink.html&quot;);\n    }\n}<\/code><\/pre>\n<p>\u5728DeepLinkActivity\u83b7\u53d6\u4f20\u9012\u7684\u503c<\/p>\n<pre><code class=\"language-java\">public class DeepLinkActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(@Nullable Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_deep_link);\n        TextView tv = findViewById(R.id.tv);\n        \/\/\u83b7\u5f97\u7f51\u9875\u4f20\u9012\u8fc7\u6765\u7684\n        Intent intent = getIntent();\n        if (intent != null) {\n            Uri uri = intent.getData();\n            String scheme = uri.getScheme(); \/\/cn\n            String host = uri.getHost(); \/\/appblog\n            if (&quot;appblog&quot;.equals(host)) {\n                \/\/ \u8df3\u8f6c\u5230\u5bf9\u5e94\u8be6\u60c5\u9875\u9762\n                Toast.makeText(this, &quot;host:&quot;+host+&quot;scheme:&quot;+scheme+&quot;\u503c\uff1a&quot;+uri.getPathSegments(), Toast.LENGTH_SHORT).show();\n                tv.setText(&quot;host:&quot;+host+&quot;\\nscheme:&quot;+scheme+&quot;\\n\u503c\uff1a&quot;+uri.getPathSegments());\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u6548\u679c\u70b9\u51fb\u540e\u5c31\u6253\u5f00\u6211\u4eec\u5199\u7684\u8be6\u60c5\u9875\u4e86\uff0c\u6211\u662f\u5199\u5728\u4e00\u4e2aapp\u91cc\u7684\uff0c\u5f53\u7136\u53ea\u8981\u662fwebview\u5c31\u53ef\u4ee5\u6253\u5f00\u5bf9\u5e94\u7684\u8be6\u60c5\u9875\uff0c\u4e24\u4e2a\u5e94\u7528\u4e4b\u95f4\u7684\u8df3\u8f6c\u5c31\u5b8c\u6210\u4e86<\/p>\n<h2>\u5c01\u88c5\u65b9\u6cd5\u7528\u6765Activity\u4e4b\u95f4\u7684\u8df3\u8f6c<\/h2>\n<pre><code class=\"language-java\">\/**\n * deepLink\u8df3\u8f6c\n **\/\nprotected void deepLinkJump(String deepLinkUrl) {\n    Uri uri = Uri.parse(deepLinkUrl);\n    Intent intent = new Intent(Intent.ACTION_VIEW, uri);\n    startActivity(intent);\n}<\/code><\/pre>\n<p>\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">deepLinkJump(&quot;cn:\/\/chat_activity&quot;); \/\/\u8bb0\u5f97\u5728\u6e05\u5355\u6587\u4ef6\u6dfb\u52a0host\u548cscheme\u54e6!<\/code><\/pre>\n<p>\u914d\u7f6e\u6e05\u5355\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;activity\n    android:name=&quot;.view.ChatActivity&quot;\n    android:launchMode=&quot;singleTask&quot;\n    &gt;\n    &lt;intent-filter&gt;\n        &lt;action android:name=&quot;android.intent.action.VIEW&quot; \/&gt;\n        &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; \/&gt;\n        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; \/&gt;\n        &lt;data\n            android:host=&quot;chat_activity&quot;\n            android:scheme=&quot;cn&quot;\n            \/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/activity&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Deeplink\u662f\u4ec0\u4e48\uff1f Deeplink\uff0c\u7b80\u5355\u8bb2\uff0c\u5c31\u662f\u5728\u4f60\u624b\u673a\u4e0a\u70b9\u51fb\u4e00\u4e2a\u94fe\u63a5\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u94fe\u63a5\u5230app\u5185\u90e8\u7684\u67d0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[481],"class_list":["post-1876","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-deeplink"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1876"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1876\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}