给出了这三个网址:
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>
此 fragment 适用于
1)
的情况和 2)
: 第一个网址没有在应用中打开,第二个是。但遗憾的是,我没有通过应用程序打开第三个链接。我还尝试了一些不同的变体
path
, pathPrefix
和 pathPattern
,但我没有运气实现所有三个给定的行为。所以我需要你们的帮助,你们能提供一个满足给定要求的 fragment 或一些我可以测试的提示吗?
更新:
更改
android:pathPrefix
至android:pathPattern
现在可以正常工作:系统的 Intent 选择器仅针对案例 2)
显示和 3)
, 案例 1)
直接打开浏览器。但是
我还想实现的是在进入应用程序或触发 Intent 选择器之前检查特定参数。这应该只在参数
param
时发生。持有值 hello
而不是 goodbye
.在 pathPattern
中使用某种正则表达式是否可能? -属性?
最佳答案
我希望这个解决方案可以帮助您解决任务。
list .xml
Don't include
android:pathPrefix="/app"
in 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>
In YourActivity.kt check the Intent data to perform further action.
Note: The code is written in 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()
}
关于android - 使用包含路径和查询参数的(深度)链接打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727141/