通过NFC标签深度链接到应用程序(特定活动)

3
我正在开发一个需要与NFC标签进行深度链接的Android应用程序。
以下是该活动的意图过滤器:
<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>

现在当我在adb中启动此命令时,应用程序将以正确的活动(ScheduleActivity)开始:

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

但是当我在NFC标签上编码URL时,扫描该标签只会启动我的手机浏览器。我错过了什么来使用NFC标签启动活动?

标签上编码的URL:"http://www.testdeeplink.com/schedule?stop_id=1"

1个回答

10
您忘记在manifest文件中添加NFC意图过滤器。NFC标签上的URL将不会触发视图意图操作,而是将被发送到带有NDEF_DISCOVERED意图操作的活动。因此,您可以通过在manifest文件中添加一个额外的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+的某些设备上似乎存在一些(未经确认的?)问题,其中浏览器似乎会劫持来自NFC标签的URL,尽管NDEF意图过滤器正确。我自己目前没有遇到这个问题,所以无法进一步调查。

谢谢,你的解决方案有效,但为了确保浏览器不会劫持意图,我使用了一个特定的URL,类似于:"testdeeplink://schedule?stop_id=1"。现在它可以正常工作了。 - Sagon nicolas
@Sagonnicolas,使用自己的方案肯定可以防止浏览器处理链接。另一种选择是在标签上添加Android应用记录(AAR),在URL记录之后为您的应用程序。这也将使设备在设备上未安装该应用程序时打开您的应用程序列表。 - Michael Roland
@MichaelRoland,请问您是否有关于这个劫持问题的详细信息?我在Android 10上遇到了这个问题,但是我找不到任何解决方法(我的卡已经使用类型03/04(http/https)URL发行在NDEF数据中)。谢谢。 - vlp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接