Android to receive and send SMS implementation code _ANDROID

Source: Internet
Author: User

Android broadcasts Android.provider.Telephony.SMS_RECEIVED messages when it receives text messages, so you can receive text messages by defining a receiver and listening to the message.

<receiver android:name= ". Smsreceiver" > 

  <intent-filter> 

    <action android:name= " Android.provider.Telephony.SMS_RECEIVED "/> 

  </intent-filter> 

</receiver>

You can also add a priority to Intent-filter:

<intent-filter android:priority= ">"

This allows the application to receive the Android.provider.Telephony.SMS_RECEIVED message before the system because of higher priority.
Java files:

Package com.hzhi.smsReceiver;
Import Java.text.SimpleDateFormat; 
Import Java.util.Date;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsMessage;
 

Import Android.util.Log; public class Smsreceiver extends Broadcastreceiver {@Override public void onreceive (context, Intent Intent)
     {//SMS content StringBuilder strbody = new StringBuilder ();
     SMS Time String strtime = new string ();
   SMS Sender StringBuilder Number = new StringBuilder (); 
   SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); 
   Bundle Bundle = Intent.getextras (); 
     if (bundle!= null) {object[] _pdus = (object[)) bundle.get ("PDUs"); 

     smsmessage[] message = new Smsmessage[_pdus.length];
     for (int i = 0; i < _pdus.length i++) {Message[i] = SMSMESSAGE.CREATEFROMPDU ((byte[)) _pdus[i]); for (Smsmessage currentmessage : message) {Strbody.append (Currentmessage.getdisplaymessagebody ()); 
       Number.append (Currentmessage.getdisplayoriginatingaddress ());

     Strtime = Formatter.format (New Date (Currentmessage.gettimestampmillis ())); 
     String smsbody = strbody.tostring (); 
     String Smsnumber = number.tostring ();
     LOG.V ("number=", Smsnumber); 
     LOG.V ("Time=", strtime); 

     LOG.V ("body=", smsbody);

    Cancel Message this.abortbroadcast ();

 }

  }   

}

After receiving the message, the intent Extras (bundle variable) is obtained by the Intent.getextras () method, then the SMS array is obtained using Bundle.get ("PDUs"). Why is the SMS array? This is because some SMS content is long, so need an array to store. Each element in the array, and then take out the message content, SMS source number, SMS time. Among them, these elements of the SMS source number and SMS time are the same, SMS content is not the same, need to be spliced together to form a complete text message content.

Finally, This.abortbroadcast () cancels the Android.provider.Telephony.SMS_RECEIVED message. If a higher priority is set for Intent-filter, the application receives the Android.provider.Telephony.SMS_RECEIVED message before the system, and then discards the message, The system will not receive Android.provider.Telephony.SMS_RECEIVED messages, and will not send messages to the tone, vibration and so on. If you do not set the priority, the system will first receive Android.provider.Telephony.SMS_RECEIVED message, send out a message beep, vibration, and so on, and then receive text messages.

Send SMS public
void Sendmsg () {
  String content = Edtsend.gettext (). toString ();
  Smsmanager Smsmanager = Smsmanager.getdefault ();
  list<string> dividecontents = smsmanager.dividemessage (content);
  for (String text:dividecontents) {  
    smsmanager.sendtextmessage (smswidget.str_number, NULL, text, NULL, NULL);

}

First get a Smsmanager instance, then use the Dividemessage () method to divide the content of the text into several parts, because if the text message content is too long, may exceed the maximum allowable length of the transmission, then you need to divide the text into several strings, In general, the content will not exceed the maximum length, so it is a string; Finally, for all strings, use the Sendtextmessage () method to send, where smswidget.str_number is the offset number, and text is each string.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.