Android send SMS and receive SMS

Source: Internet
Author: User

 Packagecom.example.testsms;Importandroid.app.Activity;Importandroid.app.PendingIntent;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateButton Send; PrivateTextView number; PrivateTextView content; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Send= (Button) This. Findviewbyid (R.id.button1); number= (TextView) This. Findviewbyid (R.ID.TEXTVIEW2); Content= (TextView) This. Findviewbyid (R.id.textview1); Intentfilter Intentfilter=NewIntentfilter (); Intentfilter.addaction ("com"); Myreceiver Myreceiver=NewMyreceiver ();        Registerreceiver (Myreceiver,intentfilter); Send.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubSmsmanager Smsmanager =Smsmanager.getdefault (); Intent sentintent=NewIntent ("com"); Pendingintent pendingintent= Pendingintent.getbroadcast (mainactivity. This, 0, sentintent, 0); Smsmanager.sendtextmessage (String) Number.gettext (),NULL, (String) Content.gettext (), Pendingintent,NULL);            }        }); }    classMyreceiverextendsbroadcastreceiver{@Override Public voidonreceive (Context arg0, Intent arg1) {//TODO auto-generated Method Stub            if(Getresultcode () = =RESULT_OK) {Toast.maketext (mainactivity). This, "Success", 1). Show (); }        }    }}
 Packagecom.example.mysms6;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.telephony.SmsMessage;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView TextView1; PrivateTextView textView2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView1= (TextView) This. Findviewbyid (R.id.textview); TextView2= (TextView) This. Findviewbyid (R.ID.TEXTVIEW3); Intentfilter Intentfilter=NewIntentfilter (); Intentfilter.setpriority (800); Intentfilter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); Myreceiver Myreceiver=NewMyreceiver ();    Registerreceiver (Myreceiver,intentfilter); }    classMyreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {Bundle bundle=Intent.getextras (); Object[] PDUs= (object[]) bundle.get ("PDUs")); Smsmessage[] Messages=NewSmsmessage[pdus.length];  for(intI= 0;i<messages.length;i++) {Messages[i]= SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]); } String Address= Messages[0].getoriginatingaddress (); String Fullmessage= "";  for(Smsmessage message:messages) {fullmessage+=Message.getmessagebody ();            } textview1.settext (address);            Textview2.settext (Fullmessage);        Abortbroadcast (); }    }}

It's convenient for you to check it later. One with Eclipse one with Android Studio

Android send SMS and receive SMS

Related Article

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.