【问题标题】:Android NFC Intent-filter for all type适用于所有类型的 Android NFC 意图过滤器
【发布时间】:2015-03-03 17:18:54
【问题描述】:

我想创建一个 Android 应用来处理所有类别和所有数据类型发现的所有 NFC 事件,例如 NDEF、TECH 和 TAG。

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

<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 不要调用我的应用程序。

谁能发现我做错了什么?

【问题讨论】:

  • 您的应用程序在前台运行吗?如果是这样,您应该使用其他方式
  • 没有。我想在程序不在前台时显示 NFC 数据。

标签: android android-intent nfc intentfilter ndef


【解决方案1】:

你的意图过滤器

<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 的意图调度是如何工作的(请参阅 How NFC Tags are Dispatched to Applications

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

  2. NDEF_DISCOVERED 意图过滤器需要(在许多 平台版本/设备上,在某些平台上是可选的)您要侦听的附加数据类型(请参阅&lt;data ... /&gt;)。没有像包罗万象的NDEF_DISCOVERED 意图过滤器这样的东西(尽管您可以通过将TECH_DISCOVERED 用于 Ndef 和 NdefFormatable 技术来接近它)。 NDEF_DISCOVERED 只会匹配最具体的意图过滤器。例如,如果您注册所有以“http://”开头的 URL,则任何注册以“http://www.example.com/”开头的 URL 的应用都将优先于您的应用。因此,您需要注册 无限 种数据类型才能获得高于所有其他应用的优先权。

  3. TECH_DISCOVERED 意图过滤器需要额外定义您要侦听的标记技术(请参阅LaurentYanswer)。可用的技术是命名空间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 文件中指定它们。例如,要匹配所有 NfcA 和所有 NfcB 标签,您可以在名为 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>
    

    然后您可以使用&lt;meta-data&gt; 标记(在&lt;activity&gt; 标记内但在&lt;intent-filter&gt; 标记外部附加此XML 文件:

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

【讨论】:

  • 我试过这个方法。如果我尝试使用纯文本会很好,因为我会捕获所有 MIME 类型,例如 text/*、audio/*、... 等。如何获取智能海报,例如 URL、电子邮件或名片数据?
  • @grillcsirke 对于 URL,您可以使用带有 android:scheme(可能还有 android:host、android:path 等)属性的 &lt;data ...&gt; 元素。对于名片,您通常会过滤 vCard MIME 类型。
  • 标签与休息的良好解释。
【解决方案2】:

您必须像这样创建多个意图过滤器:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
   <action android:name="android.nfc.action.TECH_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/filter_nfc" />
<intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

对于 TECH_DISCOVERED,您必须创建一个 XML 资源文件,以指定您的活动在技术列表集中支持的技术,如下所述:http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多