NFC Technology: Let Android automatically open Web pages

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.