• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

扫描 NFC 标签时是否可以启动应用程序?

用户头像
it1352
帮助188

问题说明

我有一个 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?

正确答案

#1

为了让你的应用(实际上是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.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):

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 暗示 NfcANfcB,
  • MifareClassic 隐含 NfcA,
  • MifareUltralight 隐含 NfcA,并且
  • Ndef/NdefFormatable 暗示 NfcANfcBNfcFNfcV.
  • IsoDep implies either NfcA or NfcB,
  • MifareClassic implies NfcA,
  • MifareUltralight implies NfcA, and
  • Ndef / NdefFormatable imply either NfcA, NfcB, NfcF, or NfcV.

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangfeagk
系列文章
更多 icon
同类精品
更多 icon
继续加载