2015-12-14 75 views
8

我能找到的最接近的是这个问题here。但它并不完全覆盖我所遇到的问题。不遵循路径前缀的Android深层链接

我在应用程序设置中使用/ app作为路径前缀的深层链接。我遇到的问题是像http://example.com/upgrade这样的链接也试图在我的应用程序中打开,即使它在url中没有/ app任何位置。我知道你不能排除由前缀指定的网址,但不是路径前缀的全部要点只包括那些网址?

基本上我想这样的深层链接链接:

http://example.com/app/home 
http://example.com/app/specials 

而不是链接这样的:

http://exaple.com/ 
http://example.com/login 

和这里是我在我的清单:

<data android:scheme="http" 
    android:host="example.com" 
    android:pathPrefix="/app"/> 

编辑1

还发现this link但我没有任何空前缀,仅只是一个斜线一“/”

编辑2

这是触发这是http://example.com/upgrade.php?app=1&method=1&uid=1的URL,我不知道,如果之后的应用程序?也会触发它,所以我改变了前缀到/应用程序,但也没有工作,它仍然触发它们。

编辑3

这里是清单中的其他深层链接数据标签:

姿态的活动

<data android:scheme="myapp" 
    android:host="profile" 
    android:pathPrefix="/"/> 

登录/注册活动

<data android:scheme="myapp" 
    android:host="login" 
    android:pathPrefix="/signup"/> 

主要活动

<data android:scheme="myapp" 
    android:host="main" 
    android:pathPrefix="/"/> 

<data android:scheme="http" 
    android:host="test.example.com" 
    android:pathPrefix="/app"/> 

<data android:scheme="http" 
    android:host="live.example.com" 
    android:pathPrefix="/app"/> 

编辑4

这是越来越混乱,如果我的myapp删除数据标签从活动方案(或者,如果我从一切去除pathPrefix用的“前缀/“),它不再触发来自网址的深层链接,即使它们中有/ app。

+0

这是您的清单中唯一的''标签吗? –

+0

我有3个活动,还有两个活动各有一个。 – JStephen

+0

这是使用example.com的唯一一个,其他人使用我的应用程序名称作为方案,其中一些使用“/”作为前缀,但所有使用http方案的方案都有一个定义的前缀“/ app” – JStephen

回答

1

跑我自己的一些测试中,我觉得你自己已经创造了这个问题,继续测试您的应用程序更多的深层链接去

设置之前 - >您的应用程序名称 - >按默认启动并选择清除默认

然后导航回到您的应用清单和意图过滤器添加到您的活动的每个网址:

在你的榜样,你问

http://example.com/app/home 
http://example.com/app/specials 

我打算假设你有一个活动,它会为上面给出的URI启动其中的每一个。

为HomeActivity清单中做到:

<activity android:name=".HomeActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 

     <!-- http://example.com/app/home --> 
     <data android:scheme="http" 
      android:host="example.com" 
      android:pathPrefix="/app/home"/> 

     <!-- https://example.com/app/home --> 
     <data android:scheme="https" 
      android:host="example.com" 
      android:pathPrefix="/app/home"/> 

     <!-- custom url scheme example://app/home --> 
     <data android:scheme="example" 
      android:host="app" 
      android:pathPrefix="/home"/> 
    </intent-filter> 
</activity> 

adb shell am start -W -a android.intent.action.VIEW -d http://example.com/app/homeadb shell am start -W -a android.intent.action.VIEW -d example.com/app/home测试从终端

然后你SpecialsActivity与adb shell am start -W -a android.intent.action.VIEW -d http://example.com/app/specialsadb shell am start -W -a android.intent.action.VIEW -d example.com/app/specials

<activity android:name=".SpecialsActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 

     <!-- http://example.com/app/specials --> 
     <data android:scheme="http" 
      android:host="example.com" 
      android:pathPrefix="/app/specials"/> 

     <!-- https://example.com/app/specials --> 
     <data android:scheme="https" 
      android:host="example.com" 
      android:pathPrefix="/app/specials"/> 

     <!-- custom url scheme example://app/specials --> 
     <data android:scheme="example" 
      android:host="app" 
      android:pathPrefix="/specials"/> 
    </intent-filter> 
</activity> 

测试从终端

其实这就是它是如何工作的,如果你设置你的应用程序来处理http://example.com/路径,选择默认选择的话,以及它要尝试在短期使用你的应用程序

所以开http://example.com/{anything},当您指定pathPrefix,确保你包含了你真正想拦截的内容,并且如果你测试了一个不应该打开你的应用的链接,那么默认情况下不要选择你的应用,只需调整你的pathPrefix,这样它就不会被你的应用触发。上述

例如网址不会开放

http://example.com/app 

,因为我们还没有定义一个<data />标签来处理这个问题。

UPDATE 而使用\或何时从文档直接添加*:

因为'\'用作当字符串从XML读取(之前它解析为一个模式转义字符),则需要双重转义:例如,字面'*'将写为'\\*',而文字'\'将写为'\\\\'。这与在Java代码中构建字符串时需要编写的基本相同。

祝你好运,快乐的编码!

+0

这可能会工作,除了我也有一些现有的深度链接,使用自定义方案没有前缀,就像'example :// main /'刚刚进入主页面,然后在主要活动中打开特定片段,例如'example:// main/settings'这听起来像我将不得不摆脱第一个,只是有前缀永远ything。但那需要我改变我的深层链接,一旦他们在那里,说起来容易做起来难。 – JStephen

8

我明白了,看起来好像数据标签是相互渗透的,所以带有方案“example”的数据的前缀“/”也适用于其他数据标签中的所有方案。我不得不使用单独的意图过滤器,为我的自定义方案深层链接和网址深层链接就像这样:

<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" /> 

     <!-- must start with http://test.example.com/app --> 
     <!-- http://test.example.com/ won't work since prefix/is in a different intent-filter --> 
     <data android:scheme="http" 
      android:host="test.example.com" 
      android:pathPrefix="/app"/> 

     <!-- must start with http://test.example.com/app --> 
     <data android:scheme="http" 
      android:host="live.example.com" 
      android:pathPrefix="/app"/> 

    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 

     <!-- must start with example://main/ --> 
     <!-- http://test.example.com/ won't work since http is in a different intent-filter --> 
     <data android:scheme="example" 
      android:host="main" 
      android:pathPrefix="/"/> 

    </intent-filter> 
</activity> 
0

正如所解释的here,你的属性获得合并:

尽管它可能包括多个<data>元素位于同一个过滤器中,因此当您的意图是声明唯一的URL(例如方案和主机的特定组合)时,创建单独的过滤器非常重要,因为同一个意图过滤器中的多个<data>元素实际上会合并为一个帐户他们的组合属性的所有变化。

因此将它们放入单独的<intent-filter>实例中可以修复您的问题。