适用于所有类型的Android NFC Intent过滤器 [英] Android NFC Intent-filter for all type

查看:145
本文介绍了适用于所有类型的Android NFC Intent过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Android应用程序,处理针对所有类别和所有数据类型发现的所有NFC事件,例如NDEF,TECH和TAG.

I would like to create an Android app that handle all of the NFC event like NDEF, TECH and TAG discovered for all categories and all data types.

这些意图过滤器位于我的Android清单文件中:

These intent filters are in my Android Manifest file:

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

当事件为TAG_DISCOVERED时,此代码有效. NDEF_DISCOVERED不要调用我的应用.

This code works when the event is TAG_DISCOVERED. NDEF_DISCOVERED don't call my app.

任何人都可以发现我在做什么错吗?

Can anyone spot what I'm doing wrong?

推荐答案

您的意图过滤器

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

由于NFC的意图调度是如何工作的,因此没有多大意义(请参见如何将NFC标签分配给应用程序)

does not make much sense due to the fact how intent dispatching for NFC works (see How NFC Tags are Dispatched to Applications)

    仅当为匹配标签的TECH_DISCOVEREDNDEF_DISCOVERED意向注册了应用程序时,才会触发
  1. TAG_DISCOVERED(在清单中使用).因此,如果您还打算注册您的应用程序以处理所有TECH_DISCOVEREDNDEF_DISCOVERED意图,则通常无需同时注册TAG_DISCOVERED.

  1. TAG_DISCOVERED (when used in the manifest) will only ever be fired if no app is registered for a TECH_DISCOVERED or NDEF_DISCOVERED intent that matches the tag. Hence, if you also intend to register your app to handle all TECH_DISCOVERED and NDEF_DISCOVERED intents, there is typically no need to also register for TAG_DISCOVERED.

NDEF_DISCOVERED意图过滤器需要(在许多平台版本/设备上,某些情况下是可选的)其他您要侦听的数据类型(请参见TECH_DISCOVERED与之接近). NDEF_DISCOVERED将仅匹配最特定的意图过滤器.例如,如果您注册以"http://"开头的所有URL,则任何注册以" http://www.example.com/"将优先于您的应用.因此,您需要注册无穷数量的数据类型,才能获得高于所有其他应用程序的优先级.

The NDEF_DISCOVERED intent filter requires (on many platform versions/devices, optional on some) an additional data type that you want to listen for (see <data ... />). There is no such thing as a catch-all NDEF_DISCOVERED intent filter (though you can get close to that by using TECH_DISCOVERED for Ndef and NdefFormatable technologies). NDEF_DISCOVERED will only match the most specific intent filter. For example, if you register for all URLs that start with "http://", any app that registers for URLs starting with "http://www.example.com/" will get precedence over your app. Thus, you would need to register for an endless number of data types in order to get precedence over all other apps.

TECH_DISCOVERED意图过滤器需要您要侦听的标签技术的其他定义(请参见 LaurentY 答案).可用的技术是名称空间android.nfc.tech.*中的那些技术,当前:

The TECH_DISCOVERED intent filter requires an additional definition of tag technologies that you want to listen for (see LaurentY's answer). 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文件中指定它们.例如,要匹配所有NfcA和所有NfcB标签,您可以在名为xml/nfc_tech_filter.xml:

You specify them in an XML file. For instance, to match all NfcA and all NfcB tags, you could use this in a file called 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>
</resources>

然后您可以使用<meta-data>标记(在<activity>标记内,但在外部 <intent-filter>标记内)附加此XML文件:

You can then attach this XML file using the <meta-data> tag (within the <activity> tag but outside the <intent-filter> tag:

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

这篇关于适用于所有类型的Android NFC Intent过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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