2016-09-21 63 views
8

Android有a guide about how to implement app links.也就是说,如果我的应用程序声明它处理某些网络链接,并且我尝试在任何其他应用程序中打开此链接,系统会拦截此应用程序并将用户直接带到我的应用程序,而不是浏览器,因此我可以直接在我的应用程序中显示相关内容。非常便利。如何使用通配符域实现Android应用程序链接?

我在引导我缺少的是两两件事:

  1. 如何实现与通配符域的应用链接。我希望我的应用能够处理* .example.com的链接,即example.com(test.example.com,something.example.com等)的subdomains的所有链接;

  2. 如何仅在我的网站上实现应用链接到特定路径。例如,我想截取test.example.com/something,但不是test.example.com/other。第一个应该到我的应用程序,另一个到我的浏览器;

The corresponding iOS guide显示,iOS的处理这两种情况的(虽然通配符的部分是从文档不清楚,我有你需要放置在根域的相关文件苹果的支持,而不是一个子域澄清)。

Android应用程序链接可以处理通配符域和只有一部分路径吗?

+1

对于后者,[尝试'机器人:所述''元件上path'](https://developer.android.com/guide/topics/清单/数据element.html)。 – CommonsWare

+0

关于通配符域/主机,我在这里看到了一些对Intent Filter源中的主机通配符的引用 - https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/ IntentFilter.java。在总结说不能完成之前,也许有人可以看看这个问题,看看它在这个问题的背景下是否相关。 – Jaanus

+0

如果你在'AuthorityEntry'上引用'mWild'等,'match()'实现看起来不正确。但是,除此之外,在''/'IntentFilter'中使用通配符功能是必要的,但还不够。过滤器可能支持通配符,但应用链接不支持。 – CommonsWare

回答

9
  1. 遗憾的是,似乎Android的无法处理通配符域

如果你看一下在数据标签(https://developer.android.com/guide/topics/manifest/data-element.html)的API指南,你可以看到他们提到通配符可用于pathPattern和mime类型,而不是主机。

的事情是,正如CommonsWare在其他岗位上的主体(https://stackoverflow.com/a/34068591/4160079),

的域在安装时检查解释,也没有办法添加新的领域,除了通过航运带有新清单的新版应用程序。

因此,您必须手动列出所有可用的子域,并在每次启动新子域时更新应用程序。

这里是你如何申报多个子:

<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> 
  • 是的,你只能处理路径
  • 一个子集,这是同样的想法,只需使用路径属性(同样参见数据上面的标记API指南)列出所需的路径。

    如果您使用查询字符串或路径参数,更喜欢使用pathPrefix

    如有必要,您可以在此处使用通配符,方法是选择pathPattern

    URI的路径部分,必须以/开头。路径属性指定与Intent对象中完整路径匹配的完整路径。 pathPrefix属性指定仅与Intent对象中路径的初始部分匹配的部分路径。 pathPattern属性指定与Intent对象中的完整路径匹配的完整路径,但它可以包含以下通配符: 星号('')将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> 
    
    +0

    请参阅我对通配符域问题的评论。似乎Intent过滤器源中有一些与通配符相关的代码。这是相关的吗? – Jaanus

    +1

    您提到的与通配符相关的代码适用于自定义方案。 但是,在App Links的情况下,安装后会立即执行每个域的验证。 – APE

    3

    Android无法像现在这样处理通配符域(按照他们今天的文档),但是,这将回答您对包含和排除某些/路径的查询。

    要实现深层链接网址的的喜欢 -

    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> 
    

    现在考虑的是你能做到这一点,这里是你如何限制访问到部分应用内容 -

    <?xml version="1.0" encoding="utf-8"?> 
    <search-engine xmlns:android="http://schemas.android.com/apk/res/android"> 
        <noindex uri="http://example.com/gizmos/hidden_uri"/> 
        <noindex uriPrefix="http://example.com/gizmos/hidden_prefix"/> 
        <noindex uri="gizmos://hidden_path"/> 
        <noindex uriPrefix="gizmos://hidden_prefix"/> 
    </search-engine> 
    

    And The Manifest part-

    <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.Gizmos"> 
        <application> 
         <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"/> ... 
         </activity> 
          <meta-data android:name="search-engine" android:resource="@xml/noindex"/> 
        </application> 
        <uses-permission android:name="android.permission.INTERNET"/> 
    </manifest> 
    

    有关详细信息和这个例子说明,你可以看看AT-

    Android Deep Linking

    希望它能帮助,快乐编码

    +0

    我在这里没有看到有关通配符域的任何信息。 – Jaanus

    +0

    Android无法处理通配符域,但是,这是为了包含和排除某些* /路径*。 – bozzmob

    相关问题