Two ways to send SMS to Android

Source: Internet
Author: User

First: Call the system SMS interface to send text messages directly; The main code is as follows:

/*** Direct SMS Interface Text message * *@paramPhoneNumber *@parammessage*/     Public voidsendsms (String phonenumber, String message) {//Get SMS ManagerAndroid.telephony.SmsManager Smsmanager =Android.telephony.SmsManager. Getdefault (); //Split SMS content (SMS length limit)List<string> dividecontents =smsmanager.dividemessage (message);  for(String text:dividecontents) {smsmanager.sendtextmessage (PhoneNumber,NULL, text, SENTPI, DELIVERPI); }    }

Second: To set up the system to send SMS functions; The main code is as follows:

 /**   * system Text message function *   @param   PhoneNumber *   @param  Span style= "color: #008000;" > message  */ public  void   Dosendsmsto (String phonenumber,string message) { if   (Phonenumberutils.isglobalphonenumber (PhoneNumber)) {Intent Intent = new  Intent (intent.action_sendto, Uri.parse ("Smsto:" +phonenumber));                      Intent.putextra ( "sms_body" , message);        StartActivity (Intent); }    }

Here is the first method, the first method can monitor the sending status and the receiver status of the use of more.

The status codes returned by the processing are as follows:

  

//processing the Sent status returnedString sent_sms_action = "Sent_sms_action"; Intent sentintent=NewIntent (sent_sms_action); SENTPI= Pendingintent.getbroadcast ( This, 0, Sentintent,0); //Register the broadcast receivers         This. Registerreceiver (NewBroadcastreceiver () {@Override Public voidonreceive (Context _context, Intent _intent) {Switch(Getresultcode ()) { CaseActivity.RESULT_OK:Toast.makeText (mainactivity. This,                "SMS sent Successfully", Toast.length_short). Show ();  Break;  Casesmsmanager.result_error_generic_failure: Break;  CaseSmsmanager.result_error_radio_off: Break;  CaseSMSMANAGER.RESULT_ERROR_NULL_PDU: Break; }            }        }, NewIntentfilter (sent_sms_action)); //processing the received status returnedString delivered_sms_action = "Delivered_sms_action"; //Create the Deilverintent parameterIntent deliverintent =NewIntent (delivered_sms_action); Deliverpi= Pendingintent.getbroadcast ( This, 0, Deliverintent,0);  This. Registerreceiver (NewBroadcastreceiver () {@Override Public voidonreceive (Context _context, Intent _intent) {toast.maketext (mainactivity. This,          "The addressee has successfully received the message.", Toast.length_short). Show (); }        }, NewIntentfilter (delivered_sms_action));

Don't forget the question of permissions:
<uses-permission android:name= "Android.permission.SEND_SMS"/>

Description of the parameters for sending SMS:

Smsmanager.sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent)

--destinationaddress: Target phone number
--scaddress: SMS Center number, test can not fill
--Text: SMS Content
--Sentintent: Send-to-mobile------------return send success or failure signal--subsequent processing i.e., this intent wraps the message sending status information
--Deliveryintent: Send-to-China Mobile----------Returns whether the message received this information--and subsequent processing: This intent to wrap the message is received by the other party status information (the supplier has been sent successfully, but the other party did not have received).


Source: http://download.csdn.net/detail/zyw_java/8917057

Two ways to send SMS to Android

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.