-
Notifications
You must be signed in to change notification settings - Fork 13
Home
| Home | Reading NFC Tags | Writing NFC Tags | Creating NdefMessages | Exception Handling |
|---|---|---|---|---|
| How it works | Reading | Writing | NdefMessages | Exceptions |
The Android OS will, when your application's manifest defines that you want to receive certain intents, dispatch this just as if you download a file of a certain type, and have no default installed. It will present the user the choice with which application he wants to open the data on the NFC tag.
If you have e.g. 2 browsers, and there is a web link on the tag, the user will receive a dialog with applications able to open it.
This happens as long as the current application is NOT in the Foreground Dispatch Mode.
###What is Foreground Dispatch Mode?
Basically this tells Android NOT to dispatch the intents to other applications and instead send it to the application currently being in the foreground (hence the name).
When extending our activity, your activity will be in FGD by default, capturing all intents with action NfcAdapter.ACTION_NDEF_DISCOVERED.
This is the action which has the highest precedence, Android always tries to find a match for this type first.
What kind of intents can be delivered, and why the type matters :
Android dispatches the intents without any intermediate application, unless the application is in FGD.
In the latter case, intents are dispatched to the onNewIntent(Intent) method.
As receiving an intent always pauses your application, the onResume() method is called as well (See docs.
However, the getIntent() method in the onResume() will retrieve the original intent, not the one delivered.)
If you plan to handle your tags in the onResume(), you must call setIntent(deliveredIntent) in the onNewIntent() method.
This library contains several convenience methods, and as such it should aid you when you plan to use NFC functionality.
There is an NfcActivity, which you can easily extend and gain the already parsed messages when an NFC Tag is held near your device. Refer to the reading section for more information.
This activity automatically registers itself in the Foreground Dispatch mode in order to capture all intents launched by the Android OS with action NfcAdapter.ACTION_NDEF_DISCOVERED, and upon receiving a message, the parsed messages will be available through the method getNfcMessages().
Of course, in order to use this, you'll first have to call the super implementation of the method you're overriding.
(e.g. : onNewIntent(Intent intent))
