NFC - 如何使用NDEF_DISCOVERED启动应用程序

时间:2012-01-18 10:29:48

标签: android nfc

我正在编写一个应该检测nfc标记并自动启动的应用程序。 我已经通过使用TECH_DISCOVERED +过滤器成功完成了这项工作,但我认为更好的方法是使用NDEF_DISCOVERED。 我已将intent-filter添加到我的清单中,但它不起作用。 这是我的TECH_DISCOVERED的清单代码,有效:

    <intent-filter>    
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
    </intent-filter>
    <meta-data android:name="android.nfc.action.TECH_DISCOVERED"    
     android:resource="@xml/nfc_tech_filter" />

当我想尝试NDEF_DISCOVERED时,我尝试:

                <intent-filter>    
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain" />

对于标签,我使用“Mifare classic 1k”标签,使用市场上的NFC TagInfo应用程序编写为“智能海报”。

我做错了什么? 或者,启动我的应用程序并显示活动选择对话框的另一种方法是什么?

谢谢, 叶兰。

1 个答案:

答案 0 :(得分:3)

您正在过滤text / plain而不是智能海报的URI。 Android将智能海报转换为URI,然后您必须过滤该URI。如果检查启动的意图,可以在logcat中看到URI。对于像http://example.com/file这样的URI,请执行以下操作:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http"
            android:host="example.com"
            android:pathPrefix="/file" />
</intent-filter>

有关如何解析NDEF消息的详细信息,请参阅NFC开发指南。请阅读整篇文档,以完全了解如何过滤正确的意图:http://developer.android.com/guide/topics/nfc/nfc.html

相关问题