To use the SMS service, you must add the SMS Service permission to AndroidManifest. xml.
AndroidManifest. xml
<Uses-permission android: name = "android. permission. SEND_SMS"/> <! -- Add permissions -->
Package com. sms;
Import java. util. List;
Import android. app. Activity;
Import android. app. PendingIntent;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. telephony. SmsManager;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity implements
Android. view. View. OnClickListener {
/** Called when the activity is first created .*/
Private EditText contact;
Private EditText smsContent;
Private Button commit;
Private String strNo;
Private String strSms;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
InitView ();
OnClickListener ();
}
Public void initView (){
Contact = (EditText) findViewById (R. id. contact );
SmsContent = (EditText) findViewById (R. id. smsContent );
Commit = (Button) findViewById (R. id. commit );
}
Public void onClickListener (){
Commit. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. commit:
StrNo = contact. getText (). toString ();
StrSms = smsContent. getText (). toString ();
// TODO Auto-generated method stub
/*
* Android. telephony. gsm. SmsManager should be used before android 2.0.
* Android. telephony. SmsManager;
*/
// Obtain the default SMS manager of the system.
SmsManager smsManager = SmsManager. getDefault ();
PendingIntent sentIntent = PendingIntent. getBroadcast (
MainActivity. this, 0, new Intent (), 0 );
// If the number of words exceeds 70, you need to split the text message into multiple messages for sending.
// Split the text message according to the maximum number of characters in each text message
If (strSms. length ()> 70 ){
List <String> msgs = smsManager. divideMessage (strSms );
For (String msg: msgs ){
/*
* Send SMS
*
* SmsManager. sendTextMessage (destinationAddress, scAddress,
* Text, sentIntent, deliveryIntent)
*
* -- DestinationAddress: The target phone number.
*
* -- ScAddress: the number of the SMS center. This parameter can be left blank during the test.
*
* -- Text: text message content
*
* -- SentIntent: Send --> China Mobile failed to send --> return a message indicating successful or failed sending -->
* Subsequent processing means that this intent encapsulates the message sending status information.
*
* -- DeliveryIntent: Send --> China Mobile sent successfully -->
* Whether the other party receives the message --> subsequent processing
* That is, this intent encapsulates the status information received by the other party (the supplier has successfully sent the message but the other party has not received it ).
*/
SmsManager. sendTextMessage (strNo, null, msg, sentIntent,
Null );
}
} Else {
SmsManager. sendTextMessage (strNo, null, strSms, sentIntent,
Null );
}
Toast. makeText (MainActivity. this, "message sent successfully", Toast. LENGTH_LONG)
. Show ();
Break;
}
}
}