Android can send text messages in two ways
First, call the system's text message function. The main code is as follows:
Uri uri = Uri.parse("smsto:10010"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "102"); activity.startActivity(it);
Second, call the system SMS interface to send text messages directly. The main code is as follows:
// Directly call the SMS interface to send a text message smsmanager = smsmanager. getdefault (); List <string> dividecontents = smsmanager. dividemessage (content); For (string text: dividecontents) {smsmanager. sendtextmessage ("150 XXXXXXXX", null, text, sentpi, deliverpi );}
Here we will mainly explain the second method. It is good for the user experience to directly send without redirecting:
The first line of code above is to get the SMS manager,
The second line is to split the text message content, because the text message has limits on the number of words
The third to the fifth line is actually sending text messages,
The parameters for sending text messages are as follows:
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- Destinationaddress: The target phone number. Who do you want to send it?
-- Scaddress: the number of the SMS center. This parameter can be left empty for testing.
-- Text: Text message content
-- Sentintent: Send --> China Mobile failed to send --> return sending success or failure signal --> subsequent processing
That is, this intent encapsulates the message sending status and determines whether the message is sent successfully.
-- Deliveryintent: Send --> China Mobile successfully sent --> return whether the recipient receives the message --> subsequent processing
That is, this intent encapsulates the status information received by the recipient and whether the recipient has received the message (the supplier has sent the message successfully, but the recipient has not received it ).
After the system sends a text message, the returned status must be processed. The Code is as follows:
Processing 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 receiverscontext. registerreceiver (New broadcastreceiver () {@ override public void onreceive (context _ context, intent _ intent) {Switch (getresultcode () {Case activity. result_ OK: Toast. maketext (context, "message sent successfully", toast. length_short ). show (); break; Case smsmanager. result_error_generic_failure: break; Case smsmanager. result_error_radio_off: break; Case smsmanager. result_error_null_pdu: Break ;}}, new intentfilter (sent_sms_action ));
Processing the returned receiving status
String delivered_sms_action = "delivered_sms_action"; // create the deilverintent parameterintent 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 recipient has successfully received", toast. length_short ). show () ;}, new intentfilter (delivered_sms_action ));
Author: jason0539
Weibo: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)