NFC technology: Transferring text using Android Beam technology (i)

Source: Internet
Author: User

1 //achieve two Android phone text transfer2 //4.0 or more3 //method: Two Android phone to back, a mobile phone input text, click on the screen to send, another receive display4 5  Public classMainactivityextendsActivityImplements6 Createndefmessagecallback, Onndefpushcompletecallback {7 8     PrivateNfcadapter Mnfcadapter;9     PrivateEditText Mbeamtext;Ten     Privatependingintent mpendingintent; One  A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.fragment_main); -Mbeamtext =(EditText) Findviewbyid (R.ID.EDITTEXT1); -Mnfcadapter = Mnfcadapter.getdefaultadapter ( This); -Mpendingintent = Pendingintent.getactivity ( This, 0,NewIntent ( This, +GetClass ()), 0); -  +         //specifies the callback to transfer text AMnfcadapter.setndefpushmessagecallback ( This, This); at         //Transfer Completion Call -Mnfcadapter.setonndefpushcompletecallback ( This, This); -     } -  - @Override -     protected voidOnresume () { in         //TODO auto-generated Method Stub -         Super. Onresume (); to         if(Mnfcadapter! =NULL) { +Mnfcadapter.enableforegrounddispatch ( This, Mpendingintent,NULL, -                     NULL); the         } *     } $ Panax Notoginseng @Override -     protected voidOnPause () { the         //TODO auto-generated Method Stub +         Super. OnPause (); A         if(Mnfcadapter! =NULL) { theMnfcadapter.disableforegrounddispatch ( This); +         } -     } $  $     //display the received data -     voidprocessintent (Intent Intent) { -  theparcelable[] Rawmsgs =Intent - . Getparcelablearrayextra (nfcadapter.extra_ndef_messages);WuyiNdefmessage message = (ndefmessage) rawmsgs[0]; theString Text = Textrecord.parse (message.getrecords () [0]). GetText (); -Toast.maketext ( This, text, 0). Show (); Wu     } -  About @Override $     protected voidonnewintent (Intent Intent) { -         //TODO auto-generated Method Stub -         //super.onnewintent (intent); - processintent (intent); A     } +  the @Override -      Public voidOnndefpushcomplete (Nfcevent event) { $         //TODO auto-generated Method Stub theLOG.I ("----->>", "Done"); the     } the  the @Override -      Publicndefmessage Createndefmessage (nfcevent event) { in         //TODO auto-generated Method Stub the  theString Text =Mbeamtext.gettext (). toString (). Trim (); About         if("". Equals (text)) { theText = "Default Text"; the             //when the phone is near another phone, make it automatically turn on the calculator the             //ndefmessage ndefmessage = new Ndefmessage ( +             //New ndefrecord[] {Ndefrecord -             //. Createapplicationrecord ("Com.android.calculator2")}); the             //Bayi  theNdefmessage Ndefmessage =NewNdefmessage ( the                     Newndefrecord[] {creattextrecord (text)}); -             returnNdefmessage; -         } the  the         return NULL; the     } the  -      PublicNdefrecord Creattextrecord (String text) { the  the         byte[] Langbytes =Locale.CHINA.getLanguage (). GetBytes ( theCharset.forname ("Us-ascii"));94Charset utfencoding = Charset.forname ("UTF-8"); the         byte[] Textbytes =text.getbytes (utfencoding); the         intUtfbit = 0; the         CharStatus = (Char) (Utfbit +langbytes.length);98         byte[] data =New byte[1 + langbytes.length +Textbytes.length]; AboutData[0] = (byte) status; -System.arraycopy (langbytes, 0, data, 1, langbytes.length);101System.arraycopy (textbytes, 0, data, 1 +Langbytes.length,102 textbytes.length);103 104Ndefrecord Ndefrecord =NewNdefrecord (Ndefrecord.tnf_well_known, theNdefrecord.rtd_text,New byte[0], data);106         returnNdefrecord;107 108     }109  the}

NFC technology: Transferring text using Android Beam technology (i)

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.