Xml code:
Java code:
Package com. baidu. smssend; import android. support. v7.app. actionBarActivity; import android. telephony. smsManager; import android. text. textUtils; import android. annotation. suppressLint; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. toast; @ SuppressLint ("ShowToast") public class MainActivity extends ActionBarActivity implements OnClickListener {// declare the Button and two editText objects: Button button1; EditText et_num, et_content; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the component Id button1 = (Button) findViewById (R. id. button1); et_num = (EditText) findViewById (R. id. et_num); et_content = (EditText) findViewById (R. id. widget38); // set the listener button1.setOnClickListener (this) for the button;} // click the event @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: // get the content String num = et_num.getText () in two edittexts (). toString (). trim (); String content = et_content.getText (). toString (). trim (); // if the number or content is null, a warning is issued if (TextUtils. isEmpty (num) | TextUtils. isEmpty (content) Toast. makeText (this, R. string. warning1, 1 ). show (); else {// get the SMS manager and import the package Android. telephony. smsManagerSmsManager sm = SmsManager. getDefault (); // The system sends a text message APIsm. sendTextMessage (num, null, content, null, null) ;}break; default: break ;}}}
Add permission: