--smsmanager of system services provided by Android (SMS manager)
--Reprint Please specify Source: Coder-pig
Smsmanager Related introduction and use diagram:
Of course, in order to facilitate everyone, put the code sticky, you do not bother to write code:
Copy and paste it when you need it!
1) Call the system to send SMS functions:
public void Sendsmsto (String phonenumber,string message) { //Determines whether the input phonenumber is a legitimate phone number if ( Phonenumberutils.isglobalphonenumber (PhoneNumber)) {//uri.parse ("Smsto") here is converted to the specified Uri, fixed notation Intent Intent = 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:
public void Sendsms (String phonenumber,string message) { //Get SMS Manager Android.telephony.SmsManager Smsmanager = Android.telephony.SmsManager.getDefault (); Split SMS content (SMS length limit), seemingly limited to 140 characters, that is, //can only send 70 Chinese characters, more to split into a number of SMS send //45th parameters, if there is no need to listen to send status and receive status can write null list<string> dividecontents = smsmanager.dividemessage (message); for (String text:dividecontents) { smsmanager.sendtextmessage (phonenumber, NULL, text, SENTPI, deliverpi); }
Pendingintent to handle the sending status:
Processing the returned send status String sent_sms_action = "Sent_sms_action"; Intent sentintent = new Intent (sent_sms_action); Pendingintent SENTPI = pendingintent.getbroadcast (context, 0, sentintent, 0); The broadcast recipient Context.registerreceiver (new Broadcastreceiver () { @Override public void OnReceive (Context _) that is registered to send the message 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: //Common error break; Case Smsmanager.result_error_radio_off: //radio broadcast is explicitly closed for break; Case SMSMANAGER.RESULT_ERROR_NULL_PDU:/ /not provided pdubreak;case Smsmanager.result_error_no_service:/ / The service is currently unavailable for break;}} , New Intentfilter (sent_sms_action));
Pendingintent to handle the receive state:
Handles the returned receive status String delivered_sms_action = "Delivered_sms_action"; Create a Intent Intent deliverintent = new Intent (delivered_sms_action) that receives the received status returned ; Pendingintent Deliverpi = pendingintent.getbroadcast (context, 0,deliverintent, 0); Context.registerreceiver (New Broadcastreceiver () { @Override public void OnReceive (Context _context, Intent _ Intent) { Toast.maketext (context, "Recipient has successfully received", Toast.length_short). Show ();
--smsmanager of system services provided by Android (SMS manager)