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
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 components under the current view
Button sendBtn = (Button) findViewById (R. id. send );
// Register an event
SendBtn. setOnClickListener (this );
}
@ SuppressWarnings ("deprecation ")
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
EditText user = (EditText) findViewById (R. id. user );
EditText content = (EditText) findViewById (R. id. content );
// Information management object
SmsManager smsManager = SmsManager. getDefault ();
// The normal intent state of the Request Code represented by the three parameters after intent
PendingIntent intent = PendingIntent. getBroadcast (SendMessageActivity. this, 0, new Intent (), 0 );
// If the number of characters exceeds 70, you need to split it into multiple messages for sending.
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 );
}
// Prompt message sent successfully
Toast. makeText (SendMessageActivity. this, "message sent successfully", Toast. LENGTH_LONG). show ();
}
}
Note: during the test, if the content. length () value is not set to greater than 4, five messages will be sent in two messages. 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.

 

Author w_l_j

Related Article

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.