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:
and I receive only this and no print at all....
01-31 16:37:18.980: ERROR/MediaPlayer(990): setAudioStream called in state 8
01-31 16:37:18.980: ERROR/MediaPlayer(990): error (-38, 0)
01-31 16:37:18.980: ERROR/MediaPlayer(990): start called in state 0
01-31 16:37:18.980: ERROR/MediaPlayer(990): error (-38, 0)
01-31 16:37:18.988: ERROR/MediaPlayer(990): Error (-38,0)
When I use activity to handle the intent like this:
<activity android:name="TagViewer"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The activity is started and working perfectly, so how can I make it work with BroadcastReceiver?
A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme. Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With