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)