2016-12-28 78 views
2

我正在開發一款需要與NFC標籤進行深層鏈接的Android應用。通過NFC標籤深入鏈接到應用(特定活動)

在這裏你可以看到我的意圖過濾器的活動:

<activity 
    android:name=".ui.schedule.ScheduleActivity" 
    android:parentActivityName=".ui.home.HomeActivity"> 

    <intent-filter android:label="testDeepLink"> 
     <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.testdeeplink.com" 
      android:pathPrefix="/schedule"/> 

    </intent-filter> 

</activity> 

現在,當我在亞行的應用程序啓動這個命令用正確的活動(ScheduleActivity)開始:

adb shell am start -W -a android.intent.action.VIEW -d "http://www.testdeeplink.com/schedule?stop_id=1" com.exmemple.android 

但是,當我使用NFC標籤對網址進行編碼,掃描該標籤即可啓動手機的網絡瀏覽器。使用NFC標籤開始活動時錯過了什麼?

URL標籤上的編碼:「http://www.testdeeplink.com/schedule?stop_id=1

回答

1

你缺少把一個NFC意圖過濾器在您的清單。 NFC標籤上的網址不會觸發意圖操作VIEW。相反,他們將被髮送到意向操作NDEF_DISCOVERED的活動。因此,你可以把你的清單NDEF_DISCOVERED行動的額外意圖過濾器收到這樣的NFC意圖:

<activity 
    android:name=".ui.schedule.ScheduleActivity" 
    android:parentActivityName=".ui.home.HomeActivity"> 

    <intent-filter android:label="testDeepLink"> 
     <action android:name="android.intent.action.VIEW" /> 
     ... 
    </intent-filter> 
    <intent-filter android:label="testDeepLinkNFC"> 
     <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="http" 
       android:host="www.testdeeplink.com" 
       android:pathPrefix="/schedule" /> 
    </intent-filter> 

注意這裏似乎是運行Android 6.0以上版本,其中一些(未經證實?)問題與一些設備儘管使用了正確的NDEF意圖過濾器,但瀏覽器似乎劫持了NFC標籤中的網址。到目前爲止,我沒有親身體驗過這個,所以我不能進一步調查這一點。

+0

謝謝,您的解決方案的工作,但要確保瀏覽器沒有劫持的意圖,我已經使用了一個特定的網址,如:「testdeeplink:// schedule?stop_id = 1」 現在它的工作 –

+0

@ Sagonnicolas是的,使用你自己的方案肯定會阻止瀏覽器處理鏈接。另一種方法是在標籤上的URL記錄*之後爲您的應用添加Android應用記錄(AAR)。如果應用程序未安裝在設備上,這也會使設備在應用程序中打開應用程序。 –

相關問題