Purpose of Use: Current activity can respond directly to an NFC tag without requiring the user to choose all the activity that can be handled.
Steps to use:
First step: In the OnCreate () method
// NFC Front-desk dispatch system Private null;
...
......
// initializes the pendingintent, and when an NFC device is connected, it is given to the current activity processing
Pendingintent = pendingintent.getactivity (Thisnew Intent (This, GetClass ()). Addflags ( Intent.flag_activity_single_top), 0);
Step two: OnPause (), Onresume (), and the Onnewintent () method add the following code.
@Overrideprotected voidOnPause () {if(Nfcadapter! =NULL) Nfcadapter.disableforegrounddispatch ( This); Super. OnPause (); } @Overrideprotected voidOnresume () {Super. Onresume (); if(Nfcadapter! =NULL) Nfcadapter.enableforegrounddispatch ( This, Pendingintent, filters, tenchlists); } @Overrideprotected voidonnewintent (Intent Intent) {Super. Onnewintent (Intent); //The current app is running in the front-end interface, this time there are intent sent over, then the system will call the Onnewintent callback method, the intent sent over//all we need to do here is check if this intent is an NFC-related intent, and if so, call the processing methodTag tag = Intent.getparcelableextra (Nfcadapter.extra_tag);//get tag tags that can handle relevant information if(NfcAdapter.ACTION_TECH_DISCOVERED.equals (Intent.getaction ())) {LOG.D ("H_bl", "Onnewintent"); Praseintent (Intent); } }
Part Three: Key steps: The Onresume () function has two parameters that are not yet replenished.
void Android.nfc.NfcAdapter.enableForegroundDispatch (activity activity, pendingintent intent, Intentfilter [] filters, string[][] techlists)
Enable foreground dispatch to the given Activity.
This would give give the foreground activity when dispatching a discovered to an Tag
application.
If any intentfilters is provided to this method they is used to match dispatch Intents for both the ACTION_NDEF_DISCOVERED
and ACTION_TAG_DISCOVERED
. Since ACTION_TECH_DISCOVERED
relies on meta data outside of the intentfilter matching for that dispatch Intent are handled by passing in the Tech lists separately. Each first level entry in the tech list represents a array of technologies that must all is present to match. If any of the first level sets match then the dispatch is routed through the given pendingintent. In other words, the second level was anded together and the first level entries was ORed together.
If you pass for both the and null
filters
parameters that techLists
acts a wild card and would cause the foreground activity to Receive all tags via the ACTION_TAG_DISCOVERED
intent.
This method must was called from the main thread, and only if the activity is in the foreground (resumed). Also, activities must call disableForegroundDispatch(Activity)
before the completion of their onPause()
callback to disable foreground dispatch after it had been enabled.
Filters:the intentfilters to override dispatching for, or null to always dispatch
Techlists:the Tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED
intent
Publicstring[][] tenchlists; Publicintentfilter[] Filters; ...
......tenchlists=NewString[][] {{ISODEP.class. GetName ()}, {NFCV.class. GetName ()}, {NFCF.class. GetName ()},}; Try{Filters=NewIntentfilter[] {NewIntentfilter (nfcadapter.action_tech_discovered, "*/*") }; } Catch(malformedmimetypeexception E1) {e1.printstacktrace (); }
Filters: The current activity can filter the NFC tag.
Techlists: An array of NFC tag technologies that the application wants to process. --an array of NFC tag technologies to be processed (obtained, can not be processed, does not respond to this tag).
4.NFC Front-Desk dispatch system