Android NFC tag received with broadcastreceiver

前端 未结 2 1412
慢半拍i
慢半拍i 2020-12-16 17:46

I\'m trying to catch NFC tag in broadcast receiver so I wrote a simple BR that prints \"asd\" in the onReceive(). In the manifest xml it\'s desribed like that:

相关标签:
2条回答
  • 2020-12-16 17:53

    You can't. As you pointed out, the NFC adapter is using something very much like startActivity() to send out an intent with tag information in it. It's not exactly like what we can do within the Android SDK, since NFC tags are special. For instance, you cannot emulate the startActivity() on your own for anything except TAG_DISCOVERED, which is the action of last resort and not terribly useful.

    I think the reason for this is due to the special handling of NFC intents. When a tag is discovered by the NFC hardware, it goes looking for something that will handle the tag. Foreground activities get first try. It tries an NDEF_DISCOVERED intent next if it can, and looks for an activity to take it. If it can't find one, it tries an intent with TECH_DISCOVERED. Again, if no activity can be found, it finally tries TAG_DISCOVERED. If it used a broadcast, how could it do this fall-back logic to keep trying to find something to handle the tag? How would it know if anything was acting on the tag intent? And how could it ensure that only one thing was going to act on the tag?

    0 讨论(0)
  • 2020-12-16 17:56

    You could write a small activity that doesn't show any UI at all, sends a broadcast message and then ends with finish(). Using flags in the manifest you can avoid it showing up in history or in recents and being faily invisible, hopefully getting a similar effect to that you desire with the braodcast receiver.

    0 讨论(0)
提交回复
热议问题