Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NFC tag received with broadcastreceiver

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?

like image 623
z_z_z_z Avatar asked Jan 31 '11 16:01

z_z_z_z


People also ask

What is a BroadcastReceiver in Android?

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.

How pass data from BroadcastReceiver to activity in Android?

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.

What is the use of BroadcastReceiver?

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.

What is the method name in BroadcastReceiver receive the message?

onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast.


2 Answers

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?

like image 115
Dave MacLean Avatar answered Nov 13 '22 04:11

Dave MacLean


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.

like image 31
Andrew Mackenzie Avatar answered Nov 13 '22 04:11

Andrew Mackenzie