Android create simple send and receive SMS Applications _android

Source: Internet
Author: User

This example for you to share the Android create send and receive SMS application of the simple implementation process for your reference, the specific content as follows

First, receive SMS

The structure of the project is as follows: A simple function of receiving and sending SMS

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

The broadcast
class Messagereceiver extends Broadcastreceiver {@Override to receive the SMS message is public
  void OnReceive Context, Intent Intent) {
    Bundle Bundle = Intent.getextras ();
    Object[] PDUs = (object[]) bundle.get ("PDUs"); Extract SMS Message
    smsmessage[] messages = new Smsmessage[pdus.length];
    for (int i = 0; i < messages.length i++) {
      Messages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[)) pdus[i]);
    String address = messages[0].getoriginatingaddress (); Gets the sender number
    String fullmessage = "";
    for (Smsmessage message:messages) {
      Fullmessage + = Message.getmessagebody ()//Get SMS content
    }
    Textview.settext ("address;" + address + ", message:" + fullmessage);
    Abortbroadcast ();
  }

2, register the broadcast, here we dynamically registers in the code:

Register to receive broadcast
Intentfilter intentFilter1 = new Intentfilter ();
Intentfilter1.setpriority (intentfilter.system_high_priority);
Intentfilter1.addaction ("Android.provider.Telephony.SMS_RECEIVED");
Messagereceiver = new Messagereceiver ();
Registerreceiver (Messagereceiver, intentFilter1);

3, declare the right:

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

Second, send SMS

1, send SMS: Getdefault method to get Smsmanager object, call Sendtextmessage method to send SMS

Send SMS public
void Send (view view) {
  Smsmanager Smsmanager = Smsmanager.getdefault ();
  String destinationaddress = "15527100207";
  String Text = "I love you!" ";

  Intent intent1 = new Intent (sent_sms_action);
  Pendingintent sentintent = pendingintent.getbroadcast (this, 0, intent1, 0);
  Intent Intent2 = new Intent (delivery_sms_action);
  Pendingintent deliveryintent = pendingintent.getbroadcast (this, 0, Intent2, 0);
  Smsmanager.sendtextmessage (destinationaddress, NULL, text, sentintent, deliveryintent); Aidl Services, interprocess communication
}

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

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 words is the default smsc
text://        Send the details of the message
sentintent:     //When the message succeeds or fails to send, it initiates this broadcast
deliveryintent:   //When the message arrives at its destination, We're going to launch this broadcast.

2, Sentintent Broadcast:

Send SMS Status Broadcast
class Messagebroadcast extends Broadcastreceiver {
  @Override public
  void OnReceive ( Context, Intent Intent {
    //a 10-digit number to send, can also be successful, but the other side did not receive text messages.
    if (getresultcode () = = RESULT_OK) {
      toast.maketext (mainactivity.this, "Send success!", Toast.length_short). Show () ;
    } else {
      Toast.maketext (mainactivity.this, "Send failed!", Toast.length_short). Show ();
  

3, Deliveryintent Broadcast:

 
class Deliverybroadcast extends Broadcastreceiver {
  @Override public void onreceive When the message is passed to the recipient
  ( Context, Intent Intent) {
    toast.maketext (mainactivity.this, "The other side receives SMS success!", Toast.length_short). Show ();
}

The above is the entire content of this article, I hope to help you learn.

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