如何使用通配符域实现Android应用程序链接?
Android有这样的功能,即如果我的应用程序声明它处理某些web链接,并且我尝试在任何其他应用程序中打开此链接,系统将拦截此链接并直接将用户带到我的应用程序,而不是浏览器,以便我可以直接在我的应用程序中显示相关内容。非常方便 我在指南中遗漏了两件事:如何使用通配符域实现Android应用程序链接?,android,ios,applinks,Android,Ios,Applinks,Android有这样的功能,即如果我的应用程序声明它处理某些web链接,并且我尝试在任何其他应用程序中打开此链接,系统将拦截此链接并直接将用户带到我的应用程序,而不是浏览器,以便我可以直接在我的应用程序中显示相关内容。非常方便 我在指南中遗漏了两件事: 如何使用通配符域实现应用程序链接。我希望我的应用程序处理指向*.example.com的链接,即指向example.com(test.example.com、something.example.com等)的子域的所有链接 如何实现仅指向我的网站上
子域的所有链接
Android应用程序链接能否处理通配符域和路径的子集?Android无法处理通配符域(根据今天的文档),但这将回答您关于包含和排除某些/路径的查询 为类似URL的应用程序实现深度链接-
http://example.com/gizmos?1234,
http://example.com/gizmos/1234,
http://example.com/gizmos/toys/1234,
etc.
您的XML应该如下所示-
<activity android:name="com.example.android.GizmosActivity" android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_title_viewgizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <!-- Accepts URIs that begin with "example://gizmos” -->
<data android:scheme="example" android:host="gizmos" />
</intent-filter>
<intent-filter android:label="@string/filter_title_viewgizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://example.com/gizmos” -->
<data android:scheme="http" android:host="example.com" android:pathPrefix="/gizmos" />
</intent-filter>
</activity>
希望有帮助,快乐编码
更新:现在您可以使用数字资产链接处理通配符域
整个过程记录如下:
总之,现在可以在主机标记中使用通配符,并且必须将名为assetlinks.json的json文件上传到根域上的/.well文件夹/路由
或者,如果使用通配符(例如*.example.com)声明主机名,则必须在根主机名(example.com)处发布assetlinks.json文件
您还需要将属性android:autoVerify=“true”添加到您的意图过滤器标签中
以下是Android端的整个示例:
<application>
<activity android:name=”MainActivity”>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="*.example.com" />
</intent-filter>
</activity>
</application>
),您可以看到他们提到通配符可用于pathPattern和mimeType,但不适用于主机
问题是,正如Commonware在另一篇关于这个主题()的文章中所解释的那样
这些域在安装时会被检查,除了使用新清单发布新版本的应用程序外,没有办法添加新域
因此,您必须手动列出所有可用子域,并在启动新子域时更新应用程序
以下是如何声明多个子域:
<activity android:name="MainActivity">
<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" />
<data android:host="subdomain1.example.com" />
<data android:host="subdomain2.example.com" />
<data android:host="subdomain3.example.com" />
</intent-filter>
</activity>
是,您只能处理路径的子集
这是相同的想法,只需使用path属性列出您想要的路径(同样,请参阅上面的数据标记API指南)
如果使用查询字符串或路径参数,则首选使用pathPrefix
如有必要,您可以在此处使用通配符,方法是选择pathPattern
URI的路径部分,必须以/开头。路径属性指定与意图对象中的完整路径匹配的完整路径。pathPrefix属性指定仅与意图对象中路径的初始部分匹配的部分路径。pathPattern属性指定与意图对象中的完整路径匹配的完整路径,但它可以包含以下通配符:
星号(“”)与前一个字符的0到多个匹配序列。
后跟星号(“.”)的句点匹配0到多个字符的任何序列
这里有几个例子:
<activity android:name="MainActivity">
<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" />
<data android:host="subdomain1.example.com" />
<data android:host="subdomain2.example.com" />
<data android:host="subdomain3.example.com" />
<data android:path="/path1" /> <!-- matches /path1 only -->
<data android:pathPrefix="/path2" /> <!-- matches /path2, /path2/something or also /path2?key=value etc... -->
<data android:pathPattern="/wild.*" /> <!-- matches /wild, /wild3, /wilderness etc... -->
</intent-filter>
</activity>
引用自:
或者,如果使用通配符(例如*.example.com)声明主机名,则必须在根主机名(example.com)处发布assetlinks.json文件。例如,只要assetlinks.json文件发布在https://example.com/.well-known/assetlinks.json上,具有以下意图过滤器的应用程序将通过example.com(如foo.example.com)任何子名称的验证:
<application>
<activity android:name=”MainActivity”>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="https" android:host="mobile.example.com" />
</intent-filter>
</activity>
</application>
对于后者,。关于通配符域/主机,我在这里的Intent Filter源代码中看到了一些对主机通配符的引用-。在得出无法完成的结论之前,也许有人可以看看这一点,看看它是否与这个问题相关。如果您指的是mWild
等AuthorityEntry
,那么match()
实现似乎不正确。但是,除此之外,在
/IntentFilter
中使用通配符功能是必要的,但还不够。可能筛选器支持通配符,但应用程序链接不支持。我不知道最近是否有此文档的更新,但我从上面发布的链接中了解到,它确实支持数据标记上主机属性的wildacard。我在这里没有看到任何关于通配符域的内容。Android无法处理通配符域,但是,这是为了包含和排除某些/路径。请参阅我对通配符域问题的评论。在意图过滤器源代码中似乎有一些与通配符相关的代码。这是否相关?您提到的与通配符相关的代码适用于自定义方案。但是,对于应用程序链接,在安装后会立即执行每个域的验证。主机也可以使用通配符。根据您共享的链接。@MayurMore您是对的-看起来自从我发布我的答案后,这已经更新了。我将编辑我的答案-同时,我建议查看au
<activity android:name="MainActivity">
<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" />
<data android:host="subdomain1.example.com" />
<data android:host="subdomain2.example.com" />
<data android:host="subdomain3.example.com" />
<data android:path="/path1" /> <!-- matches /path1 only -->
<data android:pathPrefix="/path2" /> <!-- matches /path2, /path2/something or also /path2?key=value etc... -->
<data android:pathPattern="/wild.*" /> <!-- matches /wild, /wild3, /wilderness etc... -->
</intent-filter>
</activity>
<application>
<activity android:name=”MainActivity”>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="https" android:host="mobile.example.com" />
</intent-filter>
</activity>
</application>