Android Basics Getting Started Tutorial--10.2 Smsmanager (SMS manager)

Source: Internet
Author: User

Android Basics Getting Started Tutorial--10.2 Smsmanager (SMS manager)

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

This section brings in the Android Smsmanager (short-term manager), known as the name, is used to manage mobile phone messages,
And this kind of application scenario is not many, generally we send text messages when the API will be used, of course, this text message is
Text text message, for MMS is too complex, and in QQ various social app rampage of age, you will send 1 dollars a piece of
MMS? So in this section we only discuss sending plain text text messages!
Official Document: Smsmanager

1. Call the system to send SMS function:

is to write the recipient and content sent to the system to send a text message interface, the user to verify that the recipients of the correct content is true and then click Send!
To be blunt is to call the system to send a text message window, this has certain advantages:
This text message, the app installs the time can write less a text message permission , then such as 360 this kind of security software when installs the time
Will not remind users: "This app has text message permission, may be secretly drop text messages Oh", and users of the act of secretly texting is very
Disgust, of course some people do not look directly installed, and some people may feel that will secretly send text messages Oh, good disgusting application, I don't pretend,
Or simply prohibit our app to send text messages, then when the app sends a text message, there may be some anomalies, or
Application direct crash and so on! So if your app needs to send text messages to verify or pay for these things, it's recommended!

Core Code :

publicvoidSendSMSTo(String phoneNumber,String message){        //判断输入的phoneNumber是否为合法电话号码      if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){          //Uri.parse("smsto") 这里是转换为指定Uri,固定写法          new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));                intent.putExtra("sms_body", message);                      startActivity(intent);        }    }    
2. Call the system-provided SMS interface to send SMS

This requires text message permission
< uses-permission android:name= "Android.permission.SEND_SMS" />
We directly call Smsmanager to send SMS to the SMS interface we provide:
sendtextmessage (destinationaddress, scaddress, text, Sentintent, deliverintent); The
parameter is followed by:

    • destinationaddress: The telephone number of the addressee.
    • scaddress: SMS Center number, NULL if using the current default SMS service center
    • text: SMS Content
    • sentintent: Message Sending status information: (Send status of intent)
      If it is not NULL, the Pendingintent is broadcast when the message is successfully sent or failed. The result code is ACTIVITY.RESULT_OK
      Indicates success, or Result_error_generic_failure, Result_error_radio_off, RESULT_ERROR_NULL_PDU
      One represents an error. The corresponding result_error_generic_failure,sentintent may include an additional "error code" that contains a
      Radio broadcasting technology-specific values that are usually only useful in repairing a failure. Each SMS-based application controls the detection of sentintent.
      If Sentintent is empty, the caller will detect all unknown applications, which will cause a smaller number of SMS to be sent at the time of Detection.
    • deliverintent: Whether the text message is received by the other party status information: (Receive status of the intent)
      If not NULL, when this text message is sent to the receiver, the pendtingintent will be broadcast,
      The PDU generated by the status report (referred to as the unit of data passed between peer levels) expands to the data ("PDU")

... So complex, what is the PDU's egg? Okay, don't tangle, just know these parameters are:
Phone number, information Center, SMS content, whether to send a successful monitoring, and whether the recipient is acceptable to listen to it!

Core Code :

 Public voidSendsms (StringPhoneNumber,StringMessage) {//Get SMS ManagerAndroid.Telephony.Smsmanager Smsmanager=Android.Telephony.Smsmanager.Getdefault ();//Split SMS content (SMS length limit), seemingly limited to a length of 140 characters, is    //can only send 70 characters, more to split into multiple text messages sent    //45th parameter, can write null if there is no need to listen for send status and receive status    List<String>Dividecontents=Smsmanager.Dividemessage (message); for (Stringtext:dividecontents) {Smsmanager.Sendtextmessage (PhoneNumber,NULL, text, SENTPI, DELIVERPI); }  }

You may also need to listen to the message is sent successfully, or whether the recipient receives the message, the following add it:

1) Handling sentintent that return send status

//processing the sent status returnedString sent_sms_action ="Sent_sms_action"; Intent sentintent =NewIntent (sent_sms_action); Pendingintent SENTPI = pendingintent.getbroadcast (Context,0, Sentintent,0);//Register the broadcast recipient who sent the messageContext.registerreceiver (NewBroadcastreceiver () {@Override       Public void OnReceive(Context _context, Intent _intent) {Switch(Getresultcode ()) { CaseActivity.RESULT_OK:Toast.makeText (Context,"SMS sent successfully", Toast.length_short). Show (); Break; CaseSmsmanager.result_error_generic_failure://Common error             Break; CaseSmsmanager.result_error_radio_off://Wireless broadcasts are explicitly closed             Break; CaseSMSMANAGER.RESULT_ERROR_NULL_PDU://No PDU provided             Break; CaseSmsmanager.result_error_no_service://service is not currently available             Break; }      }  },NewIntentfilter (sent_sms_action));

2) Handle the deliverintentthat returns the receiving State:

//processing returned receive status   String delivered_sms_action =  "delivered_sms_action" ; //create a intent that receives the returned receive State   Intent deliverintent = new  Intent (delivered_sms_action); Pendingintent Deliverpi = pendingintent.getbroadcast (context, 0 , Deliverintent,  Span class= "Hljs-number" >0 );     Context.registerreceiver (new  broadcastreceiver () {      @Override  public  void  onreceive  (Context _context, Intent _intent)     {Toast.maketext (Context, "Recipient has successfully received" , Toast.length_short). Show (); }}, new  intentfilter (delivered_sms_action)); 

Also here is the broadcast of knowledge, if you do not know the radio, you can look at:

Android Basics Getting Started tutorial--4.3.1 broadcastreceiver kind
Android Basics Getting Started tutorial--4.3.2 broadcastreceiver discovering

This section summarizes:

OK, this section describes the two ways Smsmanager send text messages ~ very simple ~ suggest or use
The first kind of solution, at least the user experience a bit better ...

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started Tutorial--10.2 Smsmanager (SMS manager)

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.