使用包含路径和查询参数的(深层)链接打开应用程序

JU5*_*1C3 9 android deep-linking android-deep-link

给出了这三个 url:

1) https://example.com

2) https://example.com/app

3) https://example.com/app?param=hello
Run Code Online (Sandbox Code Playgroud)

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

1) Should not open the app

2) Should open the app

3) Should open the app and extract the parameter's value
Run Code Online (Sandbox Code Playgroud)

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

    <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>
Run Code Online (Sandbox Code Playgroud)

1)此代码片段对于以下情况工作正常2):第一个网址未在应用程序中打开,第二个网址是。但遗憾的是第三个链接我没有被应用程序打开。

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

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

更新:

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

我还想实现的是在进入应用程序或触发意图选择器之前检查特定参数。仅当参数param保存值hello而不是时才会发生这种情况goodbye。这可以通过 - 属性内的某种正则表达式实现吗pathPattern

小智 -1

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

清单.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>
Run Code Online (Sandbox Code Playgroud)

在 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()
    }
Run Code Online (Sandbox Code Playgroud)

  • 但这意味着,即使 url `https://example.com` 不应该触发意图选择器并将我的应用程序显示为打开选项,应用程序也始终打开 (3认同)