Run Effect: First create a new Android project to define the required strings in String.xml:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <stringname= "Hello">Hello World, mainactivity!><stringname= "App_name">Send SMS Program</string> <stringname= "Str_input_phone_number">Please enter your phone number</string> <stringname= "Str_input_sms_content">Please input text message content</string> <stringname= "Str_send_sms">Send SMS</string> <stringname= "Str_remind_input_phone_number">Please enter your phone number</string> <stringname= "Str_remind_sms_send_finish">Send complete</string></Resources>
To edit the interface control code in Main.xml:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/str_input_phone_number" /> <EditTextAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/phone_number_edittext" /> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/str_input_sms_content" /> <EditTextAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/sms_content_edittext" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/str_send_sms"Android:id= "@+id/send_sms_button" /> </LinearLayout>
Finally, write the logic code to send the SMS in mainactivity:
PackageCom.sms.ui;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Phone_number_edittext=(EditText) Findviewbyid (R.id.phone_number_edittext); Sms_content_edittext=(EditText) Findviewbyid (R.id.sms_content_edittext); Send_sms_button=(Button) Findviewbyid (R.id.send_sms_button); Send_sms_button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {String Phone_number=Phone_number_edittext.gettext (). toString (). Trim (); String sms_content=Sms_content_edittext.gettext (). toString (). Trim (); if(Phone_number.equals ("") {toast.maketext (mainactivity). This, R.string.str_remind_input_phone_number, Toast.length_long). Show (); } Else{Smsmanager Smsmanager=Smsmanager.getdefault (); if(Sms_content.length () > 70) {List<String> contents =smsmanager.dividemessage (sms_content); for(String sms:contents) {smsmanager.sendtextmessage (Phone_number,NULLSmsNULL,NULL); } } Else{smsmanager.sendtextmessage (Phone_number,NULL, Sms_content,NULL,NULL); } toast.maketext (mainactivity. This, R.string.str_remind_sms_send_finish, Toast.length_short). Show (); } } }); } PrivateEditText Phone_number_edittext; PrivateEditText Sms_content_edittext; PrivateButton Send_sms_button;}
In the feature manifest file, declare the text message permission:
Android text messaging Feature