gpt4 book ai didi

适用于所有类型的 Android NFC Intent-filter

转载 作者:行者123 更新时间:2023-11-29 15:12:44 26 4
gpt4 key购买 nike

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

这些 Intent 过滤器在我的 Android Manifest 文件中:

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

谁能发现我做错了什么?

最佳答案

你的 Intent 过滤器

<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 的 Intent 调度是如何工作的(参见 How NFC Tags are Dispatched to Applications)

  1. TAG_DISCOVERED (在 list 中使用时)只有在 没有 应用程序为 TECH_DISCOVERED 注册时才会被触发或 NDEF_DISCOVERED与标签匹配的 Intent 。因此,如果您还打算注册您的应用程序来处理所有 TECH_DISCOVEREDNDEF_DISCOVERED Intent ,通常也不需要注册 TAG_DISCOVERED .

  2. NDEF_DISCOVERED Intent 过滤器需要(在许多平台版本/设备上,在某些平台版本/设备上是可选的)您想要监听的附加数据类型(参见 <data ... /> )。没有包罗万象的东西NDEF_DISCOVERED Intent 过滤器(尽管你可以通过使用 TECH_DISCOVERED 来接近 Ndef 和 NdefFormatable 技术)。 NDEF_DISCOVERED只会匹配最具体的 Intent 过滤器。例如,如果您注册所有以“http://”开头的 URL,则任何注册以“http://www.example.com/”开头的 URL 的应用都将优先于您的应用。因此,您需要注册无穷无尽的数据类型,才能优先于所有其他应用。

  3. TECH_DISCOVERED intent filter 需要你想要监听的标签技术的附加定义(参见 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>

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

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

关于适用于所有类型的 Android NFC Intent-filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28837963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com