Android深度链接无法使用多个方案

24

我遇到了以下情况。我在AndroidManifest.xml中定义了以下深度链接意图过滤器。

期望的行为是当我发现格式为http://www.domain.com/a/blabla的URL或者格式为domain/xyz的短信/电子邮件链接时,系统应该触发我的活动。

情况#1:运行良好

    <activity
        android:name=".MYActivity">
        <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="xyz"
                android:scheme="domain" />
        </intent-filter>
    </activity>

第二种情况:正常运行

     <activity
        android:name=".MYActivity">
        <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:scheme="http" 
                android:host="www.domain.com"
                android:pathPrefix="/a"
             />
        </intent-filter>
    </activity>

案例#3:不起作用

    <activity
        android:name=".MYActivity">
        <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="xyz"
                android:scheme="domain" />
             <data
                android:scheme="http" 
                android:host="www.domain.com"
                android:pathPrefix="/a"
             />
        </intent-filter>
    </activity>
任何建议/要点/帮助都会非常感激。

2
简而言之:在一个“意图过滤器”中,“主机”和“路径前缀”必须相同。您可以使用不同的“方案”在一个“意图过滤器”中复制具有不同“数据”元素的内容。例如:同一主机/路径前缀的http和https版本可以在一个意图过滤器中。 - Eugen Pechanec
@EugenPechanec,您的评论可能是这个问题的有效答案。在代码审查中,链接到答案比评论更容易 :) - Albert221
这位Google开发者按照你说的方法进行了操作。你认为Google是否更新了代码以使其正常工作? - j2emanue
5个回答

32

我在两个不同的intent filter中放置了两个深度链接,它们都起作用了!


你能否发布你的工作代码?我在处理两个不同的意图过滤器时遇到了麻烦。 - Vincent Paing
@Renges,你看到了什么错误?有什么问题? - prago
当我使用终端测试时,它可以使用HTTP和自定义协议打开,但是当我的朋友将网站上线并传递自定义URL协议时,它无法打开我的应用程序。 - Vincent Paing
是否可以分享自定义URL(深层链接模式)和您的清单。例如:我们的自定义深层链接如下所示 - 'domain://path/deeplink';我们的实际URL如下所示 - 'http://www.domain.com/a/b/path' - - prago
@Blanc,你在不同的意图过滤器中使用相同的操作和类别吗?例如:<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> - SoH
显示剩余2条评论

16

请查看<data>的文档,它说明:

在同一个<intent-filter>元素中包含的所有<data>元素都贡献到同一个过滤器中。

因此,

<intent-filter>
    <data
        android:host="xyz"
        android:scheme="domain" />
    <data
        android:scheme="http" 
        android:host="www.domain.com"
        android:pathPrefix="/a" />
<intent-filter>

被解释为等效于(不是真正的代码)

<intent-filter>
    <data
        android:host="xyz"
        android:scheme="domain"
        android:scheme="http" 
        android:host="www.domain.com"
        android:pathPrefix="/a" />
<intent-filter>

这显然存在一些矛盾,比如主机是xyz而不是www.domain.com


好的。这意味着我不能在同一个意图过滤器中有不同的主机。我会尝试将它们放在不同的意图过滤器中。感谢您的评论。 - prago
我强烈建议阅读 <data><intent-filter> 的文档。 - TWiStErRob
Android的哪个版本支持重复方案,我认为根据XSD规范是不可能的。 - whoami
@whoami 第二个代码块是伪XML,不是有效的。我只是把它放在那里以便通过看到矛盾点。 - TWiStErRob

8

来自Android官方文档网站:

虽然可以在同一个过滤器中包含多个<intent-filter>元素,但是当您的意图是声明唯一的URL时(例如特定的组合方案和主机),创建单独的过滤器非常重要,因为在同一个意图过滤器中的多个<intent-filter>元素实际上会合并在一起,以考虑其所有组合属性的变化。

您将需要创建单独的intent-filter


0
略微偏离主题,但与上面的OP问题相关,并提供了可能有助于其他人的额外信息。
如果您有跨多个清单的深度链接意图过滤器,并且想要将它们合并而不会互相覆盖,则可以在中添加唯一的android:label。

示例:

main/AndroidManifest.xml (这些深度链接适用于所有应用程序口味,包括debugprodcution等)

<intent-filter android:label="main_deeplink1">
    <data
        android:host="domain.com"
        android:scheme="https" />
<intent-filter>

debug/AndroidManifest.xml (这些深度链接仅适用于debug构建版本)

<intent-filter android:label="debug_deeplink1">
    <data
        android:host="dev.domain.com"
        android:scheme="https" />
<intent-filter>

合并后的清单文件现在将包含两个深度链接,而不会互相覆盖。


-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接