Is it possible to launch an app when an NFC tag is scanned?(扫描 NFC 标签时是否可以启动应用程序?)
问题描述
我有一个 NFC 标签.我想编写一个Android应用程序,当手机扫描NFC标签时,它会自动启动并从NFC获取数据.
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.
假设设备已打开 NFC 并且手机上没有运行其他应用程序,这应该可以工作.我发现了一些可以启动另一个应用程序的应用程序,但是我的应用程序应该可以在没有在后台运行的其他应用程序的情况下运行.
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?
推荐答案
为了让你的应用(实际上是activity)在扫描标签时启动,你需要添加一个合适的intent filter到你的应用清单.
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.
如果你想为任何标签启动你的应用程序,TECH_DISCOVERED 意图过滤器就是你想要使用的:
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>
这个意图过滤器需要一个额外的 XML 资源文件来定义你的应用应该监听的标签技术(注意 <meta-data .../> 标签 outside 意图过滤器).可用的技术是命名空间 android.nfc.tech.* 中的技术,目前:
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.IsoDepandroid.nfc.tech.MifareClassicandroid.nfc.tech.MifareUltralightandroid.nfc.tech.Ndefandroid.nfc.tech.NdefFormatableandroid.nfc.tech.NfcAandroid.nfc.tech.NfcBandroid.nfc.tech.NfcBarcodeandroid.nfc.tech.NfcFandroid.nfc.tech.NfcV
要发现任何标签,您可以像这样创建一个 XML 文件(将文件创建为 xml/nfc_tech_filter.xml):
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暗示NfcA或NfcB,MifareClassic隐含NfcA,MifareUltralight隐含NfcA,并且Ndef/NdefFormatable暗示NfcA、NfcB、NfcF或NfcV.
IsoDepimplies eitherNfcAorNfcB,MifareClassicimpliesNfcA,MifareUltralightimpliesNfcA, andNdef/NdefFormatableimply eitherNfcA,NfcB,NfcF, orNfcV.
如果没有其他应用具有更好的匹配意图过滤器,则会触发上述意图过滤器.更好的匹配是与标签上使用的数据类型匹配.因此,例如,如果您的标签包含 URL(封装在 NDEF 消息中),则触发 URL 的应用程序将优先于您的应用程序.如果您知道标签上使用的数据类型,您还可以为这些数据类型添加过滤器.例如,要匹配任何http://"和https://"网址,您可以使用:
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>
同样,如果您的标签包含 MIME 类型application/vnd.com.example",您可以使用:
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>
最后,还有一个与 NFC 相关的 Intent 过滤器:
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>
但是,您通常不会在清单中使用此意图过滤器.它仅作为后备,只有在没有其他应用程序触发技术或扫描标签的数据时才会触发.所以不需要为上面提到的TECH_DISCOVEREDintent filter添加你已经触发的这个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 标签时是否可以启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:扫描 NFC 标签时是否可以启动应用程序?
基础教程推荐
- 如何使用 YouTube API V3? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
