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