Android Basic----> send and receive SMS

Source: Internet
Author: User

  Sending and receiving text messages should be one of the most basic features of every cell phone, even if the old phone many years ago will have this feature, and Android as an excellent smartphone operating system, naturally, there is no support in this regard. Today we start by creating a simple app that sends and receives text messages.

Directory Navigation

    1. Receive SMS
    2. Send SMS
    3. Friendship Link

Receive SMS

The structure of the project is as follows: a simple ability to receive and send SMS messages

Define a radio message: When the phone receives a text message, the system emits a radio value of Android.provider.Telephony.SMS_RECEIVED, which carries all the data associated with the text message.

//receive a message broadcastclassMessagereceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {Bundle bundle=Intent.getextras (); Object[] PDUs= (object[]) bundle.get ("PDUs");//Extract SMS Messagessmsmessage[] Messages =NewSmsmessage[pdus.length];  for(inti = 0; i < messages.length; i++) {Messages[i]= SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]); } String Address= Messages[0].getoriginatingaddress ();//Get Sender NumberString fullmessage = "";  for(Smsmessage message:messages) {fullmessage+ = Message.getmessagebody ();//Get SMS Content} textview.settext ("Address;" + address + ", message:" +fullmessage);    Abortbroadcast (); }}

Second, register the broadcast, here we dynamically register in the code:

// register to receive a broadcast New Intentfilter (); intentfilter1.setpriority (intentfilter.system_high_priority); Intentfilter1.addaction (" Android.provider.Telephony.SMS_RECEIVED "new  messagereceiver (); Registerreceiver ( Messagereceiver, intentFilter1);

Iii. Rights of Claim:

<android:name= "Android.permission.SEND_SMS"/><   android:name= "Android.permission.RECEIVE_SMS"/>

Send SMS

One, send SMS: Use getdefault method to get Smsmanager object, call sendtextmessage method Send SMS

//Send SMS Public voidSend (View view) {Smsmanager Smsmanager=Smsmanager.getdefault (); String destinationaddress= "15527100207"; String text= "I love you!" "; Intent Intent1=NewIntent (sent_sms_action); Pendingintent sentintent= Pendingintent.getbroadcast ( This, 0, Intent1, 0); Intent Intent2=NewIntent (delivery_sms_action); Pendingintent deliveryintent= Pendingintent.getbroadcast ( This, 0, Intent2, 0); Smsmanager.sendtextmessage (destinationaddress,NULL, text, sentintent, deliveryintent);//Aidl Services, inter-process communication}

Let's take a specific look at the meaning of the sendtextmessage method parameter:

 Public void sendtextmessage (String destinationaddress, String scaddress, String text,pendingintent sentintent, pendingintent deliveryintent) destinationaddress:  //  destination, that is, the other person's mobile phone number scaddress:          //  Service center address, empty is the default smsctext:               //  Send the message of the specific content sentintent:/         /  When the message succeeds or fails to send, the broadcast deliveryintent is initiated:     //  when the message arrives at the destination, the broadcast is initiated

Third, the Sentintent broadcast:

//send a status broadcast of a text messageclassMessagebroadcastextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//a 10-digit number is sent and can be successful, but the other person does not receive text messages.         if(Getresultcode () = =RESULT_OK) {Toast.maketext (mainactivity). This, "Sent successfully!", Toast.length_short). Show (); } Else{toast.maketext (mainactivity). This, "Send failed!", Toast.length_short). Show (); }    }}

Iv. deliveryintent Radio:

  class extends broadcastreceiver {    @Override    public  void OnReceive (Context context, Intent Intent) {        toast.maketext (mainactivity. This, "the other receives SMS success!" , Toast.length_short). Show ();    }}

Friendship Link

Android Basic----> send 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: 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.