Activity class:
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.view.View.OnClickListener;
Import android.widget.*;
public class Smsactivity extends Activity {
Private EditText Phonetext;
Private EditText ContentText;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
phonetext= (EditText) Findviewbyid (R.id.phonetext);
contenttext= (EditText) Findviewbyid (R.id.contenttext);
Sendsms ();
}
public void Sendsms () {
Button button= (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New Smsonclick ());
}
Private Final class Smsonclick implements onclicklistener{
@Override
public void OnClick (View v) {
String Phonenumber=phonetext.gettext (). toString ();
String Content=contenttext.gettext (). toString ();
if (phonenumber==null| | Phonenumber.length () <1) {
Toast.maketext (Smsactivity.this, R.string.empty, Toast.length_short). Show ();
}else{
Smsmanager Smsmanager = Smsmanager.getdefault ();
Pendingintent sentintent = Pendingintent.getbroadcast (smsactivity.this,0, New Intent (), 0);
if (Content.length () > 70) {//If the number of words is more than 70, you need to split into multiple SMS send
List<string> msgs = smsmanager.dividemessage (content);
for (String msg:msgs) {
Smsmanager.sendtextmessage (PhoneNumber, NULL, MSG, sentintent, NULL);
The last two parameters are the broadcast intent that the SMS has sent, and the last parameter is the broadcast intent of the SMS message that the caller has received
}
} else {
Smsmanager.sendtextmessage (PhoneNumber, NULL, content, sentintent, null);
}
}
}
}
}
Manifest adding SMS Permission
<uses-permission android:name= "Android.permission.SEND_SMS"/>