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