enableReaderMode 和 enableForegroundDispatch 有什么区别?

What#39;s the difference between enableReaderMode and enableForegroundDispatch?(enableReaderMode 和 enableForegroundDispatch 有什么区别?)

本文介绍了enableReaderMode 和 enableForegroundDispatch 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了两种让 Android 应用检测和处理 NFC 标签的方法:

I found two approaches to let an Android app detect and process NFC tags:

  1. NfcAdapter.enableReaderMode(activity, callback, flags, extras) 然后在回调中接收标签信息.

  1. NfcAdapter.enableReaderMode(activity, callback, flags, extras) and then receive the tag info in the callback.

NfcAdapter.enableForegroundDispatch(activity, intent, filters, techLists) 然后在 onNewIntent(intent) 活动方法中接收标签信息.

NfcAdapter.enableForegroundDispatch(activity, intent, filters, techLists) and then receive the tag info in the onNewIntent(intent) activity method.

我目前使用第二种方法,但是,我最近发现了 enableReaderMode 方法,想知道用它来处理 NFC 标签是否更好.

I currently use the second approach, however, I recently discovered the enableReaderMode approach and wonder if it's better to use that to process NFC tags.

那么enableReaderModeenableForegroundDispatch有什么区别呢?

推荐答案

前台调度系统

前台调度系统(NfcAdapter.enableForegroundDispatch())从 Android 2.3.3 开始就存在(这基本上是 Android NFC 的开始).因此,所有具有 NFC 功能的 Android 设备都支持此方法.

Foreground dispatch system

The foreground dispatch system (NfcAdapter.enableForegroundDispatch()) exists since Android 2.3.3 (which is basically the beginning of Android NFC). Hence, this method is supported on all Android devices with NFC capabilities.

前台调度系统用于为当前在前台处理 NFC 发现事件(即发现的 NFC 标签和从对等设备接收的 NDEF 消息)提供优先级的活动.这意味着即使为特定标签类型或 NDEF 数据注册了另一个应用程序(通过 AndroidManifest.xml 中的意图过滤器),NFC 事件仍将被传递到前台活动的其他活动.因此,该方法不会改变 Android 侦听 NFC 设备(NFC 标签、P2P 设备)的方式,它只会改变处理已发现设备的优先级.

The foreground dispatch system is used to give an activity that is currently in the foreground precedence in handling NFC discovery events (i.e. discovered NFC tags and NDEF messages received from peer-to-peer devices). This means that even if another app is registered (by means of an intent filter in the AndroidManifest.xml) for a specific tag type or NDEF data, the NFC event will still be passed to the foreground activity instead of that other activity. Consequently, the method does not change the way Android listens for NFC devices (NFC tags, P2P devices), it only changes the priority for handling discovered devices.

阅读器模式 API (NfcAdapter.enableReaderMode()) 是在 Android 4.4 中引入的.因此,并非所有具有 NFC 功能的 Android 设备都支持此方法.

The reader-mode API (NfcAdapter.enableReaderMode()) was introduced in Android 4.4. Consequently, not all Android devices with NFC capabilities support this method.

与前台调度系统相反,阅读器模式 API 确实改变了 Android 侦听 NFC 设备的方式.reader-mode API 禁用对等模式.例如,这允许您发现同时启用了点对点模式和卡模拟模式的其他设备的卡模拟模式(如 Android HCE 的情况).(通常情况下,此类设备会被发现为点对点设备,Android 应用将无法访问卡模拟功能.)

As opposed to the foreground dispatch system, the reader-mode API does change the way Android listens for NFC devices. The reader-mode API disables peer-to-peer mode. This, for instance, permits you to discover card emulation mode of other devices that have peer-to-peer mode and card emulation mode enabled simultaneously (as is the case with Android HCE). (Normally, such a device would be discovered as a peer-to-peer device and an Android app would not be able to access the card emulation functionality.)

此外,您可以更改 NFC 阅读器模式的特定参数,例如你可以:

Moreover, you can change specific parameters of the NFC reader mode, e.g. you can:

  • 定义 NFC 读取器轮询的标签技术,
  • 通过向标签发送特定命令序列并检查是否仍收到响应来定义 Android 测试标签是否仍然存在的时间间隔,
  • 阻止 Android 自动向标签发送命令以测试标签是否包含 NDEF 消息,
  • 在标签发现时阻止 Android 播放声音.

根据 Adam Johns 的评论,上述情况在 Android 10 上可能不再适用(他测试在像素 2 上).仅使用 enableReaderMode()(没有额外的 enableForegroundDispatch())时,即使标签已正确分派,设备似乎也会显示此 NFC 标签不支持的应用程序"的祝酒词到注册的阅读器模式回调方法(onTagDiscovered()).

According to a comment by Adam Johns, the above may no longer be true on Android 10 (he tested on a Pixel 2). When using only enableReaderMode() (without an additional enableForegroundDispatch()), the devices seems to show a toast "No supported application for this NFC tag" eventhough tags are correctly dispatched to the registered reader-mode callback method (onTagDiscovered()).

这篇关于enableReaderMode 和 enableForegroundDispatch 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:enableReaderMode 和 enableForegroundDispatch 有什么区别?

基础教程推荐