Make a little progress every day-----send and receive SMS

Source: Internet
Author: User

 Packagecom.example.smstest;;;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 (; Content=(TextView) Findviewbyid (; to=(EditText) Findviewbyid (; Msginput=(EditText) Findviewbyid (; Send=(Button) Findviewbyid (; 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

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: 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.