2015-12-14 53 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>實例中可以修復您的問題。