Packagecom.example.smstest;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.telephony.SmsMessage;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateTextView Sender; PrivateTextView content; PrivateEditText to; PrivateEditText Msginput; PrivateButton Send; PrivateIntentfilter Receivefilter; PrivateMessagereceiver Messagereceiver; PrivateIntentfilter Sendfilter; PrivateSendstatusreceiver Sendstatusreceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Sender=(TextView) Findviewbyid (R.id.sender); Content=(TextView) Findviewbyid (r.id.content); to=(EditText) Findviewbyid (r.id.to); Msginput=(EditText) Findviewbyid (r.id.msg_input); Send=(Button) Findviewbyid (r.id.send); Receivefilter=NewIntentfilter (); Receivefilter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); Receivefilter.setpriority (100); Messagereceiver=NewMessagereceiver (); Registerreceiver (Messagereceiver, Receivefilter); Sendfilter=NewIntentfilter (); Sendfilter.addaction ("Sent_sms_action"); Sendstatusreceiver=NewSendstatusreceiver (); Registerreceiver (Sendstatusreceiver, Sendfilter); Send.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Smsmanager Smsmanager=Smsmanager.getdefault (); Intent sentintent=NewIntent ("Sent_sms_action"); Pendingintent Pi=pendingintent.getbroadcast (mainactivity. This, 0, sentintent, 0); Smsmanager.sendtextmessage (To.gettext (). toString (),NULL, Msginput.gettext (). toString (), Pi,NULL); } }); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Unregisterreceiver (Messagereceiver); Unregisterreceiver (Sendstatusreceiver); } classMessagereceiverextendsBroadcastreceiver {@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 (); } sender.settext (address); Content.settext (Fullmessage); Abortbroadcast (); } } classSendstatusreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {if(Getresultcode () = =RESULT_OK) {Toast.maketext (context,"Send succeeded", Toast.length_long). Show (); } Else{toast.maketext (context,"Send failed", Toast.length_long). Show (); } } }}
Make a little progress every day-----send and receive SMS