扫描NFC标签时是否可以启动应用程序? [英] Is it possible to launch an app when an NFC tag is scanned?

查看:878
本文介绍了扫描NFC标签时是否可以启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NFC标签.我想编写一个Android应用程序,当使用手机扫描NFC标签时,该应用程序会自动启动并从NFC获取数据.

这应该在设备已打开NFC且手机上没有其他应用程序运行的情况下起作用.我找到了一些可以启动另一个应用程序的应用程序,但是我的应用程序应该可以正常运行,而无需在后台运行这样的附加应用程序.

有什么办法解决这个任务吗?

解决方案

要在扫描标签时启动您的应用(实际上是 activity ),您需要向您的应用添加适当的Intent过滤器应用清单.

如果只想为任何标签启动应用程序,则要使用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>

此Intent过滤器需要一个附加的XML资源文件,该文件定义了您的应用程序应收听的标签技术(请注意,<meta-data ... />标签 outside intent-filter").可用的技术是名称空间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表示NfcANfcB
  • MifareClassic暗示NfcA
  • MifareUltralight表示NfcA,并且
  • Ndef/NdefFormatable表示NfcANfcBNfcFNfcV.

如果没有其他应用程序具有更好的匹配意图过滤器,则将触发上述意图过滤器.更好的匹配将是标签上使用的数据类型的匹配.因此,例如,如果您的标记包含一个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>

但是,您通常在清单中使用此意图过滤器.它仅是一种备用,只有在技术或扫描标签的数据没有其他应用触发时,才会触发.因此,无需为上述TECH_DISCOVERED意向过滤器添加已经触发的该意向过滤器.

I have an NFC tag. I want to write an Android application that is automatically launched and gets data from NFC when the NFC tag is scanned with the phone.

This should work assuming that the device has NFC turned on and that there are no other applications running on the phone. I found some applications which can launch another application, but my application should work without such an additional application running in the background.

Is there any way to solve this task?

解决方案

In order to get your app (actually activity) started upon scanning a tag, you need to add an appropriate intent filter to your app manifest.

If you want to start your app for just any tag, the TECH_DISCOVERED intent filter is what you would want to use:

<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>

This intent filter requires an additional XML resource file that defines the tag technologies that your app should listen to (note the <meta-data ... /> tag outside the intent-filter). The available technologies are those in the namespace android.nfc.tech.*, currently:

  • 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

To discover just any tag, you would create an XML file like this (create the file as 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>

Note that you do not necessarily need to incude the other technologies as

  • IsoDep implies either NfcA or NfcB,
  • MifareClassic implies NfcA,
  • MifareUltralight implies NfcA, and
  • Ndef / NdefFormatable imply either NfcA, NfcB, NfcF, or NfcV.

The above intent filter will be triggered if there is no other app that has a better matching intent filter. A better match would be a match for the data type used on the tag. So, for instance, if your tag contains a URL (encapsulated in an NDEF message), an app that triggers on URLs will get precedence over your app. If you know the data type(s) used on your tags, you could also add a filter for those data type(s). For instance, to match just any "http://" and "https://" URL, you could use:

<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>

Similarly, if your tag contains the MIME type "application/vnd.com.example", you could use:

<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>

You could even combine multiple intent filters for one 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>

Finally, there is one more NFC-related intent filter:

<intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

However, you would normally not use this intent filter in the manifest. It is meant as a fall-back only and will only ever be triggered if there is no other app triggering on the technology or the data of the scanned tag. So there is no need to add this intent-filter of you already trigger for the above-mentioned TECH_DISCOVERED intent filter.

这篇关于扫描NFC标签时是否可以启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆