There are two ways to send text messages in Android
First: Call the system SMS interface to send text messages directly; The main code is as follows:
Java code
- Call SMS interface to send text messages directly
- Smsmanager Smsmanager = Smsmanager.getdefault ();
- list<string> dividecontents = smsmanager.dividemessage (content);
- for (String text:dividecontents) {
- Smsmanager.sendtextmessage ("150xxxxxxxx", null, text, SENTPI, DELIVERPI);
- }
Second: To set up the system to send SMS functions; The main code is as follows:
Java code
- Uri uri = uri.parse ("smsto:10010");
- Intent it = new Intent (Intent.action_sendto, URI);
- It.putextra ("Sms_body", "102");
- Activity.startactivity (IT);
The first approach, most of which comes from the Internet, is mainly explained here :
1. Get SMS Manager
Java code
- Smsmanager Smsmanager = Smsmanager.getdefault ();
2. Split SMS content (mobile SMS length limit)
Java code
- list<string> dividecontents = smsmanager.dividemessage (content);
3. Send the split content
Java code
- list<string> dividecontents = smsmanager.dividemessage (content);
- for (String text:dividecontents) {
- Smsmanager.sendtextmessage ("150xxxxxxxx", null, text, SENTPI, DELIVERPI);
- }
4. Handling the returned send status
Java code
- 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 receivers
- 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:
- Break ;
- Case Smsmanager.result_error_radio_off:
- Break ;
- Case SMSMANAGER.RESULT_ERROR_NULL_PDU:
- Break ;
- }
- }
- }, new Intentfilter (sent_sms_action));
5. Handling the returned receive status
Java code
- String delivered_sms_action = "Delivered_sms_action";
- Create the Deilverintent parameter
- 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 addressee has successfully received it," Toast.length_short .
- . Show ();
- }
- }, new Intentfilter (delivered_sms_action));
parameter description for sending SMS
Java code
- 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-China Mobile----Send failure--return send success or failure signal--subsequent processing i.e., this intent wraps the message sending status information
-- Deliveryintent: Send--China Mobile------return to the other party to receive this information--follow-up processing i.e.: This intent to wrap the message whether the recipient received the status information (the supplier has been sent successfully, But the other party did not receive).