NFC和Intent过滤器 [英] Nfc and Intent-filter

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

问题描述

我这样写了一个nfc文本标签

I wrote a nfc text tag like this

myscheme://company?page=2&poiId=140

然后我在清单文件中创建了一个这样的Intent过滤器,以打开我的应用程序(然后使用标记信息来调用WS)

Then i've created an Intent-filter like this in my manifest file in order to open my app (and then call a WS with informations from tag)

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" android:scheme="myscheme" android:host="company"/>
</intent-filter>

如果我从数据标签中删除了schemehost参数,则一切正常,但是通过添加过滤功能,它不再起作用.

If i remove scheme and host params from data tag so everything works but by adding filtering it does not work anymore.

为什么?

有解决方案吗?

推荐答案

corvairjo 正确写道,"您正在将记录类型的MIME类型(这里是文本/纯文本)和记录类型URI混合到一个意图过滤器中.您应该只使用一个."

重点是NDEF记录由类型信息和数据有效载荷组成(实际上不止是这两个字段,但那两个字段应该足以理解其背后的概念):

The point is that an NDEF record consists of a type information and a data payload (actually there's more than just those two fields, but those two should be enough to understand the concept behind it):

+------+---------+
| TYPE | PAYLOAD |
+------+---------+

如果您创建一个包含URI的文本记录,则会得到以下内容:

If you create a text record containing your URI, you will get something like this:

+------------+-------------------------------------+
| text/plain | myscheme://company?page=2&poiId=140 |
+------------+-------------------------------------+

接收设备将根据其类型字段解释记录.因此,它将把有效载荷("myscheme://company?page = 2& poiId = 140")视为一段人类可读的文本-而 not 则作为URI!

The receiving device will interpret the record according to its type field. Therefore, it will treat the payload ("myscheme://company?page=2&poiId=140") as a piece of human-readable text -- and not as a URI!

在Android上,这意味着该记录被检测为MIME媒体类型为文本/纯文本"的一条人类可读信息.因此,您只能使用这样的意图过滤器来捕获它:

On Android, this means that the record is detected as a piece of human-readable information with the MIME media type "text/plain". Hence, you can only catch it with an intent filter like this:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

由于数据有效载荷是人类可读的文本,因此意图过滤器无法对文本的任何特定部分进行过滤(Android上的意图过滤器只能匹配类型信息和URI).

As the data payload is human-readable text, the intent filter cannot filter on any specific parts of that text (intent filters on Android can only match type information and URIs).

因此,如果要具有与URI匹配的意图过滤器,则必须将URI包装到一个记录类型中,该记录类型专门将数据有效载荷标识为URI.通常使用NFC论坛URI记录类型完成此操作:

So if you want to have an intent filter that matches on the URI, you have to wrap the URI into a record type that specifically identifies the data payload as URI. This is typically done using the NFC Forum URI record type:

+---------------+-------------------------------------+
| urn:nfc:wkt:U | myscheme://company?page=2&poiId=140 |
+---------------+-------------------------------------+

标记编写器应用程序通常提供将URI/URL写入标记的选项. Android NFC API提供了用于创建URI记录的方法NdefRecord.createUri().

Tag writer apps typically offer an option to write URIs/URLs onto tags. The Android NFC API provides the method NdefRecord.createUri() to create a URI record.

然后,您可以使用这样的意图过滤器来触发包含记录的标签:

You can then use an intent filter like this to trigger upon a tag that contains the record:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="myscheme" android:host="company" />
</intent-filter>

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

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