android - 使用包含路径和查询参数的(深度)链接打开应用程序
问题描述
给出了这三个网址:
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:pathPrefix
为android:pathPattern
现在可以正常工作:系统的意图选择器仅针对案例显示2)
,3)
案例1)
直接打开浏览器。
但
我还想实现的是在进入应用程序或触发意图选择器之前检查特定参数。这应该只在参数param
保持值hello
而不是时发生goodbye
。pathPattern
这可能在-attribute中使用某种正则表达式吗?
解决方案
我希望这个解决方案可以帮助您解决任务。
清单.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()
}
推荐阅读
- c# - 无法使用 Microsoft.Graph REST API 在根目录下创建新的 OneDrive 文件夹
- swift - SceneKit:带有多个摄像头的场景视图项目点
- javascript - 2 的补码校验和
- rust - rust 特征作为不同功能的接口
- c# - 如何使用带有 c# 的 openxml 隐藏和保护 excel 文件中的一个单元格的文本(不是整个行/列)?
- azure - 如何在应用程序洞察中处理来自 webjobs 的异常?
- ms-office - 将 VSTO 桥接到 Office.js 功能
- node.js - 请求回调,无法访问错误、响应和正文
- python - 如何在查询中访问数组的对象?
- codeigniter - 无法加载请求的文件:helpers/common_help_helper.php