Android Case Study: send multiple text messages when the number of words exceeds a certain limit

Source: Internet
Author: User
We often encounter a situation in our lives. Sometimes a text message is too long to send multiple messages. In fact, it is very easy to achieve this effect, just add a condition to judge, I will not write all the detailed steps here. I will only paste the content in the activity into package CN. csdn; import Java. util. list; import android. app. activity; import android. app. pendingintent; import android. content. intent; import android. OS. bundle; import android. telephony. GSM. smsmanager; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. Edittext; import android. widget. toast; public class sendmessageactivity extends activity implements onclicklistener {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the component button sendbtn = (button) findviewbyid (R. id. send); // register the event sendbtn. setonclicklistener (this);} @ Su Ppresswarnings ("deprecation") @ overridepublic void onclick (view v) {// todo auto-generated method stubedittext user = (edittext) findviewbyid (R. id. user); edittext content = (edittext) findviewbyid (R. id. content); // information management object smsmanager = smsmanager. getdefault (); // The normal intent state of the Request Code represented by the last three parameters: pendingintent intent = pendingintent. getbroadcast (sendmessageactivity. this, 0, new intent (), 0); // if the number of words exceeds 70, you need to split it into multiple text messages Send string con = content. gettext (). tostring (); If (content. length ()> 70) {list <string> msgs = smsmanager. dividemessage (CON); For (string MSG: MSGs) {smsmanager. sendtextmessage (user. gettext (). tostring (), null, MSG, intent, null) ;}} else {smsmanager. sendtextmessage (user. gettext (). tostring (), null, content. gettext (). tostring (), intent, null);} // The message is successfully sent toast. maketext (sendmessageactivity. this, "message sent successfully", toast. length_lon G). Show () ;}}note: I set content. Length () to greater than 4 during the test. When I send a text message, five will be divided into two short messages for sending. Only when the text message content exceeds 70 can two messages be sent. I think it may be that when the dividemessage method is called internally, multiple messages are sent only when the default value is exceeded.

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.