为了获得您的应用程序(实际上activity)在扫描标签时开始,您需要将适当的意图过滤器添加到您的应用程序清单中。
如果您只想为任何标签启动您的应用程序,TECH_DISCOVERED
意图过滤器是您想要使用的:
<activity ...>
<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" />
</activity>
此意图过滤器需要一个额外的 XML 资源文件,该文件定义您的应用程序应侦听的标签技术(请注意<meta-data ... />
tag outside意图过滤器)。可用的技术是命名空间中的技术android.nfc.tech.*
, 现在:
android.nfc.tech.IsoDep
android.nfc.tech.MifareClassic
android.nfc.tech.MifareUltralight
android.nfc.tech.Ndef
android.nfc.tech.NdefFormatable
android.nfc.tech.NfcA
android.nfc.tech.NfcB
android.nfc.tech.NfcBarcode
android.nfc.tech.NfcF
android.nfc.tech.NfcV
要发现任何标签,您可以创建一个像这样的 XML 文件(将文件创建为xml/nfc_tech_filter.xml
):
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcBarcode</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
</resources>
请注意,您不一定需要将其他技术包括在内
-
IsoDep
意味着NfcA
or NfcB
,
-
MifareClassic
暗示NfcA
,
-
MifareUltralight
暗示NfcA
, and
-
Ndef
/ NdefFormatable
暗示要么NfcA
, NfcB
, NfcF
, or NfcV
.
如果没有其他应用程序具有更好匹配的意图过滤器,则将触发上述意图过滤器。更好的匹配是与标签上使用的数据类型匹配。因此,举例来说,如果您的标签包含 URL(封装在 NDEF 消息中),则在 URL 上触发的应用程序将优先于您的应用程序。如果您知道标签上使用的数据类型,您还可以为这些数据类型添加过滤器。例如,要匹配任何“http://”和“https://”URL,您可以使用:
<activity ...>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
同样,如果您的标记包含 MIME 类型“application/vnd.com.example”,您可以使用:
<activity ...>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.com.example" />
</intent-filter>
</activity>
您甚至可以为一项活动组合多个意图过滤器:
<activity ...>
<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" />
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.com.example" />
</intent-filter>
</activity>
最后,还有一个与 NFC 相关的意图过滤器:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
但是,您通常会not在清单中使用此意图过滤器。它只是作为后备措施,并且只有在没有其他应用程序触发该技术或扫描标签的数据时才会触发。因此,无需为上述内容添加已触发的意图过滤器TECH_DISCOVERED
意图过滤器。