【问题标题】:Deep link into app (specific activity) by NFC tag通过 NFC 标签深度链接到应用程序(特定活动)
【发布时间】:2016-12-28 10:55:40
【问题描述】:

我正在开发一个需要带有 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

【问题讨论】:

    标签: android nfc intentfilter deep-linking ndef


    【解决方案1】:

    您没有在清单中放置 NFC 意图过滤器。 NFC 标签上的 URL 不会触发意图操作 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 标签中劫持了 URL。到目前为止,我自己还没有经历过这种情况,所以我无法进一步调查。

    【讨论】:

    • 谢谢,您的解决方案有效,但为了确保浏览器不会劫持我使用特定网址的意图,例如:“testdeeplink://schedule?stop_id=1” 现在它正在工作
    • @Sagonnicolas 是的,使用你自己的方案肯定会阻止浏览器处理链接。另一种方法是在标签上在 URL 记录之后 为您的应用添加 Android 应用程序记录 (AAR)。如果设备上未安装应用程序,这也会使设备在 Play 商店中打开您的应用程序。
    • @MichaelRoland 请问你是否有关于这个劫持问题的一些细节?它发生在我的 Android 10 上,我找不到任何解决方法(我的卡已经在 NDEF 数据中使用类型 03/04 (http/https) URL 发行)。谢谢
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多