1 //Enable automatic Web page opening2 //The program contains the formatting of NFC, and writes data to NFC3 //once the program is running, place the NFC tag on the back of the phone. After the toast is popped, close the program and put the NFC tag on the back of your phone to automatically open Baidu Web page4 Public classAutoopenuriactivityextendsActivity {5 PrivateNfcadapter Nfcadapter;6 Privatependingintent pendingintent;7String url = "Https://www.baidu.com";8 9 @OverrideTen protected voidonCreate (Bundle savedinstancestate) { One Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.fragment_main); -Nfcadapter = Nfcadapter.getdefaultadapter ( This); -Pendingintent = Pendingintent.getactivity ( This, 0,NewIntent ( This, theGetClass ()), 0); - - } - + @Override - protected voidOnresume () { + //TODO auto-generated Method Stub A Super. Onresume (); at //get focus, set this window to take precedence over all windows that can handle NFC - if(Nfcadapter! =NULL) { - Nfcadapter -. Enableforegrounddispatch ( This, Pendingintent,NULL,NULL); - - } in } - to @Override + protected voidOnPause () { - //TODO auto-generated Method Stub the * Super. OnPause (); $ //program exits, Windows restore defaultPanax Notoginseng if(Nfcadapter! =NULL) { -Nfcadapter.disableforegrounddispatch ( This); the } + } A the @Override + protected voidonnewintent (Intent Intent) { - //When set to "Singletop" in the manifest file, OnCreate () is called only once, and Onnewintent () calls each $ //TODO auto-generated Method Stub $ Super. Onnewintent (intent); - - //The first step is to get tag theTag Detectedtag =Intent.getparcelableextra (nfcadapter.extra_tag); - //The second step is to write the labelWuyi Writenfctag (detectedtag); the - } Wu - Private voidWritenfctag (Tag tag) { About //TODO auto-generated Method Stub $ - if(Tag = =NULL) { - return; - } ANdefmessage Ndefmessage =NewNdefmessage ( + Newndefrecord[] {ndefrecord.createuri (uri.parse (URL))}); the intSize =Ndefmessage.tobytearray (). length; - Try { $Ndef Ndef =ndef.get (tag); the if(Ndef! =NULL) { the Ndef.connect (); the //whether writable is supported the if(!ndef.iswritable ()) { - return; in the } the //determine if the data to be written can be accommodated About if(Ndef.getmaxsize () <size) { the return; the } the ndef.writendefmessage (ndefmessage); +Toast.maketext ( This, "NFC successfully written", 0). Show (); -}Else { the //new NFC needs formatting---"Formatted NFSCBayindefformatable format =ndefformatable.get (tag); the if(Format! =NULL) { the Format.connect (); -Format.format (Ndefmessage);//format and write content -Toast.maketext ( This, "NFC successfully formatted", 0). Show (); the}Else { theToast.maketext ( This, "NFC format failed", 0). Show (); the } the - } the}Catch(Exception e) { the //Todo:handle Exception the }94 } the the}
NFC Technology: Let Android automatically open Web pages