【发布时间】:2021-07-24 19:17:07
【问题描述】:
我想通过 WhatsApp 和 Telegram 等链接打开我的安卓应用。
(示例)https://chat.whatsapp.com 如果我点击此链接并安装了 WhatsApp,此链接将打开 WhatsApp 那么我该如何在我的应用程序中执行此操作?
【问题讨论】:
标签: android android-intent android-manifest deep-linking
我想通过 WhatsApp 和 Telegram 等链接打开我的安卓应用。
(示例)https://chat.whatsapp.com 如果我点击此链接并安装了 WhatsApp,此链接将打开 WhatsApp 那么我该如何在我的应用程序中执行此操作?
【问题讨论】:
标签: android android-intent android-manifest deep-linking
如果您想深度链接您的应用。例如:您打开一个链接,它应该通过您的应用程序打开。在这种情况下,我在您的应用程序中使用以webView 打开的网站。当然,您可以自定义。
开始在您的AndroidManifest.xml 中创建<intent-filter>:
<application
<activity
<intent-filter>
...
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="https"
android:host="yourURL"></data>
...
</intent-filter>
</activity>
</application>
并在您的MainActivity.java 中编写以下代码以获取要在webView 中设置的数据:
Uri uri = getIntent().getData();
if (uri!=null){
String path = uri.toString();
WebView webView = (WebView)findViewById(R.id.webView);
webView.loadUrl(path);
}
而你的webView 定义在你的activity_main.xml:
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"/>
就是这样。祝你好运!干杯:)
【讨论】:
首先你应该像这样在字符串文件中定义你的基本 URL:
<string name="base_url" translatable="false">yourapp.me</string>
之后,您应该在 Manifest 文件中定义一个 IntentFilter,如下所示:
<activity android:name=".ExampleActivity">
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="https" android:pathPrefix="/api" android:host="@string/base_url"/>
</intent-filter>
</activity>
所以你的链接应该是这样的:
https://yourapp.me/api
当您单击此链接时,它应该会在您将此意图过滤器放入其中的 Activity 中打开您的应用。
【讨论】: