首页 > 解决方案 > 使用包含路径和查询参数的(深度)链接打开应用程序

问题描述

给出了这三个网址:

1) https://example.com

2) https://example.com/app

3) https://example.com/app?param=hello

假设我在 gmail-app 中收到一封包含这三个链接的邮件,我需要以下行为:

1) Should not open the app

2) Should open the app

3) Should open the app and extract the parameter's value

到目前为止我所取得的成就:

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="example.com"
            android:pathPrefix="/app"
            android:scheme="https" />
    </intent-filter>

此代码段适用于以下情况1),并且2):第一个 url 未在应用程序中打开,第二个是。但遗憾的是,我没有通过应用程序打开第三个链接。

path我还尝试了,pathPrefix和的一些不同变化pathPattern,但我没有运气实现所有三种给定的行为。

所以我需要你们的帮助,你们能提供一个满足给定要求的片段或一些我可以测试的提示吗?

更新:

更改android:pathPrefixandroid:pathPattern现在可以正常工作:系统的意图选择器仅针对案例显示2)3)案例1)直接打开浏览器。

我还想实现的是在进入应用程序或触发意图选择器之前检查特定参数。这应该只在参数param保持值hello而不是时发生goodbyepathPattern这可能在-attribute中使用某种正则表达式吗?

标签: androiddeep-linkingandroid-deep-link

解决方案


我希望这个解决方案可以帮助您解决任务。

清单.xml

不要包含android:pathPrefix="/app"Manifest.xml中

<activity android:name=".YourActivity">
        <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="http"
                  android:host="example.com"/>
        </intent-filter>
</activity>

在 YourActivity.kt 检查 Intent 数据以执行进一步的操作。

注意:代码是用 Kotlin 编写的

    val action = intent.action
    val data = intent.dataString
    if (Intent.ACTION_VIEW == action && data != null) {
        if (data.equals("http://example.com")) {
            Toast.makeText(this, "contains only URL", Toast.LENGTH_SHORT).show()
        } else if (data.contains("http://example.com/") && !data.contains("?")) {
            Toast.makeText(this, "contains URL with pathPrefix", Toast.LENGTH_SHORT).show()
        } else if (data.contains("http://example.com/") && data.contains("?")) {
            Toast.makeText(this, "contains URL with data", Toast.LENGTH_SHORT).show()
        }
    } else {
        Toast.makeText(this, "Intent from Activity", Toast.LENGTH_SHORT).show()
    }

推荐阅读