简体   繁体   English

适用于所有类型的Android NFC Intent过滤器

[英]Android NFC Intent-filter for all type

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应用程序,处理针对所有类别和所有数据类型发现的所有NFC事件,例如NDEF,TECH和TAG。

These intent filters are in my Android Manifest file: 这些意图过滤器位于我的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>

This code works when the event is TAG_DISCOVERED. 当事件为TAG_DISCOVERED时,此代码有效。 NDEF_DISCOVERED don't call my app. NDEF_DISCOVERED不要调用我的应用程序。

Can anyone spot what I'm doing wrong? 谁能发现我在做什么错?

Your 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>

does not make much sense due to the fact how intent dispatching for NFC works (see How NFC Tags are Dispatched to Applications ) 由于NFC的意图分派是如何工作的,这没有什么意义(请参阅NFC标签如何分派给应用程序

  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. 仅当没有为匹配标签的TECH_DISCOVEREDNDEF_DISCOVERED意向注册任何应用程序时,才会触发TAG_DISCOVERED (在清单中使用)。 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 . 因此,如果您还打算注册您的应用程序以处理所有TECH_DISCOVEREDNDEF_DISCOVERED意图,通常就不需要注册TAG_DISCOVERED

  2. 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 ... /> ). NDEF_DISCOVERED意图过滤器(在许多平台版本/设备上,在某些情况下是可选的)需要您要侦听的其他数据类型(请参见<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的全部意图过滤器这样的东西(尽管您可以通过将TECH_DISCOVERED用于Ndef和NdefFormatable技术来接近它)。 NDEF_DISCOVERED will only match the most specific intent filter. NDEF_DISCOVERED将仅匹配最特定的意图过滤器。 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. 例如,如果您注册以“ http://”开头的所有URL,则任何注册以“ http://www.example.com/ ”开头的URL的应用程序都将优先于您的应用程序。 Thus, you would need to register for an endless number of data types in order to get precedence over all other apps. 因此,您需要注册无数种数据类型才能获得高于所有其他应用程序的优先级。

  3. The TECH_DISCOVERED intent filter requires an additional definition of tag technologies that you want to listen for (see LaurentY 's answer ). TECH_DISCOVERED意图过滤器需要您要侦听的标签技术的其他定义(请参阅LaurentY答案 )。 The available technologies are those in the namespace android.nfc.tech.* , currently: 可用技术是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 

    You specify them in an XML file. 您可以在XML文件中指定它们。 For instance, to match all NfcA and all NfcB tags, you could use this in a file called xml/nfc_tech_filter.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> 

    You can then attach this XML file using the <meta-data> tag (within the <activity> tag but outside the <intent-filter> tag: 然后,您可以使用<meta-data>标签(在<activity>标签内但在<intent-filter>标签外)附加此XML文件:

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

You have to create multiple intent-filter as this: 您必须创建多个意图过滤器,如下所示:

<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>

For TECH_DISCOVERED you must create an XML resource file that specifies the technologies that your activity supports within a tech-list set as explained here: http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc 对于TECH_DISCOVERED,您必须创建一个XML资源文件,该文件在技术列表集中指定您的活动支持的技术,如下所述: http : //developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech碟片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM