2016-12-28 70 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)。如果应用程序未安装在设备上,这也会使设备在应用程序中打开应用程序。 –