1. Interface layout
Interface code:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Vertical" > <TextView android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/number"/> <EditText android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "@null"Android:id= "@+id/number2"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/content"/> <EditText android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:minlines= "3"Android:id= "@+id/content"Android:hint= "@null"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button"Android:id= "@+id/button"/></linearlayout>
2. Main code
Packagecom.example.sms;Importjava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.telephony.gsm.SmsManager;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {EditText numbertext; EditText ContentText; Button button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Numbertext= (EditText) This. Findviewbyid (R.id.number2); ContentText= (EditText) This. Findviewbyid (r.id.content); Button= (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewButtonclicklistener ()); } Private Final classButtonclicklistenerImplementsview.onclicklistener{@SuppressWarnings ("Deprecation") @Override Public voidOnClick (View v) {//TODO Auto-generated method stubsString number =Numbertext.gettext (). toString (); String content=Contenttext.gettext (). toString (); Smsmanager Manager=Smsmanager.getdefault (); //If the text message is too long, can be automatically split, if the number of words more than 70, need to split into multiple SMS sentarraylist<string> texts =manager.dividemessage (content); for(String text:texts) {//Send SMS InstructionsManager.sendtextmessage (number,NULL, text,NULL,NULL); //The last parameter is 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 } //Display sent successfullyToast.maketext (mainactivity. This, R.string.success, Toast.length_long). Show (); } }}
3.string.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >sms</string > <string name= "Hello_world" >hello world!</string> <string name= "number" > Please enter your mobile phone number:< /string> <string name= "Content" > Please enter content:</string> <string name= "button" > Send </string > <string name= "Success" > Send success </string></resources>
4. Request permission (text message)
Note: After the software point sends the button, there are three ways to prompt:
Java Learning Lesson 13th, Android implementation of the ability to send text messages