Call interface in Android send SMS

Source: Internet
Author: User

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
    1. Call SMS interface to send text messages directly
    2. Smsmanager Smsmanager = Smsmanager.getdefault ();
    3. list<string> dividecontents = smsmanager.dividemessage (content);
    4. for (String text:dividecontents) {
    5. Smsmanager.sendtextmessage ("150xxxxxxxx", null, text, SENTPI, DELIVERPI);
    6. }



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

Java code
    1. Uri uri = uri.parse ("smsto:10010");
    2. Intent it = new Intent (Intent.action_sendto, URI);
    3. It.putextra ("Sms_body", "102");
    4. Activity.startactivity (IT);




The first approach, most of which comes from the Internet, is mainly explained here
:

1. Get SMS Manager

Java code
    1. Smsmanager Smsmanager = Smsmanager.getdefault ();



2. Split SMS content (mobile SMS length limit)

Java code
    1. list<string> dividecontents = smsmanager.dividemessage (content);



3. Send the split content

Java code
    1. list<string> dividecontents = smsmanager.dividemessage (content);
    2. for (String text:dividecontents) {
    3. Smsmanager.sendtextmessage ("150xxxxxxxx", null, text, SENTPI, DELIVERPI);
    4. }



4. Handling the returned send status

Java code
  1. String sent_sms_action = "Sent_sms_action";
  2. Intent sentintent = new Intent (sent_sms_action);
  3. Pendingintent SENTPI = pendingintent.getbroadcast (context, 0, Sentintent,
  4. 0);
  5. Register the broadcast receivers
  6. Context.registerreceiver (new Broadcastreceiver () {
  7. @Override
  8. public void OnReceive (Context _context, Intent _intent) {
  9. switch (Getresultcode ()) {
  10. Case ACTIVITY.RESULT_OK:
  11. Toast.maketext (Context,
  12. "SMS sent successfully", Toast.length_short)
  13. . Show ();
  14. Break ;
  15. Case Smsmanager.result_error_generic_failure:
  16. Break ;
  17. Case Smsmanager.result_error_radio_off:
  18. Break ;
  19. Case SMSMANAGER.RESULT_ERROR_NULL_PDU:
  20. Break ;
  21. }
  22. }
  23. }, new Intentfilter (sent_sms_action));



5. Handling the returned receive status

Java code
  1. String delivered_sms_action = "Delivered_sms_action";
  2. Create the Deilverintent parameter
  3. Intent deliverintent = new Intent (delivered_sms_action);
  4. Pendingintent Deliverpi = pendingintent.getbroadcast (context, 0,
  5. Deliverintent, 0);
  6. Context.registerreceiver (new Broadcastreceiver () {
  7. @Override
  8. public void OnReceive (Context _context, Intent _intent) {
  9. Toast.maketext (Context,
  10. "The addressee has successfully received it," Toast.length_short .
  11. . Show ();
  12. }
  13. }, new Intentfilter (delivered_sms_action));



parameter description for sending SMS

Java code
    1. 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).

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.