Basic Android tutorial-10.2 SmsManager (SMS Manager)

Source: Internet
Author: User

Basic Android tutorial-10.2 SmsManager (SMS Manager)
This section introduces:

In this section, SmsManager (Short Message manager) in Android is used to manage SMS messages,
There are not many such application scenarios. This API is generally used when we send text messages. Of course, this SMS is
Text messages are too complicated for MMS, and in the age of QQ social apps, you will send one piece of money
MMs? So in this section, we will only discuss sending text messages!
Official Document: SmsManager

1. Call the system's SMS sending function:

It is to send the prepared recipient and content to the system's SMS sending interface. The user verifies that the recipient's content is correct and then click send!
To put it bluntly, it is to call the system's text message window, which has some advantages:
In this way, you can send text messages during app installation.Permission to write less text messagesSo when installing security software such as 360
Users will not be reminded: "This APP has the SMS permission and may secretly send text messages ."
I hate it. Of course, some people don't want to install it directly, and some people may think that they will secretly send text messages. I don't want to install this disgusting application,
Or, if our APP is directly prohibited from sending text messages, some exceptions may occur when our APP sends text messages, or
Applications crash! If your application needs to send text messages for verification or pay for these items, we recommend that you use this method!

Core code:

Public void SendSMSTo (String phoneNumber, String message) {// determine whether the input phoneNumber is a valid phone number if (PhoneNumberUtils. isGlobalPhoneNumber (phoneNumber) {// Uri. parse (smsto) Here is the conversion to the specified Uri, fixed syntax Intent intent = new Intent (Intent. ACTION_SENDTO, Uri. parse (smsto: + phoneNumber); intent. putExtra (sms_body, message); startActivity (intent );}}
2. Call the SMS interface provided by the system to send SMS messages.

This requires the permission to send text messages.
<Uses-permission android: name = "android. permission. SEND_SMS"/>
We directly call the SMS interface provided by SmsManager to send short messages:
SendTextMessage(DestinationAddress, scAddress, text, sentIntent, deliverIntent );
The parameters are as follows:

DestinationAddress: Recipient's phone number ScAddress: The number of the SMS center. If it is null, the default SMS service center is used. Text: Text message content SentIntent: Message sending status: (Intent in sending status)
If not null, The PendingIntent is broadcast when the message is successfully sent or fails. The result code is Activity. RESULT_ OK.
Success, or RESULT_ERROR_GENERIC_FAILURE, RESULT_ERROR_RADIO_OFF, RESULT_ERROR_NULL_PDU
Indicates an error. Corresponding to RESULT_ERROR_GENERIC_FAILURE, sentIntent may include an additional "error code" that contains
Radio broadcast technology specific value, usually only useful for fault repair. Each SMS-based application control detection sentIntent.
If sentIntent is empty, the caller detects all unknown applications, which causes a small number of SMS messages to be sent during the detection. DeliverIntent: Whether the SMS is received by the recipient: (Intent in the receiving status)
If it is not null, The PendtingIntent will be broadcast when the text message is sent to the recipient,
The pdu generated by the Status Report (the data unit passed between peer-to-peer layers) will be extended to the data ("pdu ")

... So complicated. What is pdu? Well, don't worry about it. The parameters are as follows:
The phone number, Information Center, text message content, whether the listener is sent successfully, and whether the recipient accepts the listener!

Core code:

Public void sendSMS (String phoneNumber, String message) {// obtain the SMS manager android. telephony. smsManager smsManager = android. telephony. smsManager. getDefault (); // split the text message content (SMS length limit). It seems that the length is limited to 140 characters, that is, // only 70 Chinese characters can be sent, if you want to split the message into multiple SMS sending parameters, you can write a null List if you do not need to monitor the sending and receiving statuses.
  
   
DivideContents = smsManager. divideMessage (message); for (String text: divideContents) {smsManager. sendTextMessage (phoneNumber, null, text, sentPI, deliverPI );}}
  

You may also need to monitor whether the text message is sent successfully or whether the recipient has received the message. Add the following content:

1) process the returned sending statusSentIntent

// Process the returned sending status String SENT_SMS_ACTION = SENT_SMS_ACTION; Intent sentIntent = new Intent (SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent. getBroadcast (context, 0, sentIntent, 0); // register the broadcast receiver context. registerReceiver (new BroadcastReceiver () {@ Override public void onReceive (Context _ context, Intent _ intent) {switch (getResultCode () {case Activity. RESULT_ OK: Toast. makeText (context, sms sent successfully, Toast. LENGTH_SHORT ). show (); break; case SmsManager. RESULT_ERROR_GENERIC_FAILURE: // normal error break; case SmsManager. RESULT_ERROR_RADIO_OFF: // The break is explicitly disabled for wireless broadcasting; case SmsManager. RESULT_ERROR_NULL_PDU: // pdu break is not provided; case SmsManager. RESULT_ERROR_NO_SERVICE: // break is currently unavailable for the service; }}, new IntentFilter (SENT_SMS_ACTION ));

2) process and return the received statusDeliverIntent:

// Process the returned receipt status String response = DELIVERED_SMS_ACTION; // create Intent deliverIntent = new Intent (DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent. getBroadcast (context, 0, deliverIntent, 0); context. registerReceiver (new BroadcastReceiver () {@ Override public void onReceive (Context _ context, Intent _ intent) {Toast. makeText (context, the receiver has successfully received, Toast. LENGTH_SHORT ). show () ;}, new IntentFilter (DELIVERED_SMS_ACTION ));

 

 

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.