The goal we're going to achieve is: Make a text message transmitter
Interface:
Because it's a privacy issue to be involved in texting, we're going to add a line of code to the Androidmanifest.xml to get the permissions:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
Then we'll configure the Main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <textview android:layout_width= "100dp" android:layout_height= "wrap_content" android:text= "@stri Ng/number "android:id=" @+id/numberlabel "/> <edittext android:layout_width=" Fill_parent "Androi d:layout_height= "Wrap_content" android:id= "@+id/number" android:layout_torightof= "@id/numberlabel" android:layou t_aligntop= "@id/numberlabel" android:layout_marginleft= "5DP"/> <textview android:layout_width= "fill_p Arent "android:layout_height=" wrap_content "android:text=" @string/content "/> <edittext androi D:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:minlines= "3" android:id= "@+id/content" /> <button andRoid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/button" android:id= " @+id/button "/></linearlayout>
After the interface has been configured, we need to write the Java code, the use of Java code to send text message function:
Package Cn.itcast.sms;import Java.util.arraylist;import Android.app.activity;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 {private EditText numbertext; Private EditText ContentText; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Numbertext = (EditText) This.findviewbyid (R.id.number); ContentText = (EditText) This.findviewbyid (r.id.content); Button button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (New Buttonclicklistener ()); } Private Final class Buttonclicklistener implements View.onclicklistener{public void OnClick (View v) {String Numbe R = Numbertext.gettext (). toString (); String content = Contenttext.gettext (). toString (); Smsmanager ManagER = Smsmanager.getdefault (); Get the default message manager arraylist<string> texts = manager.dividemessage (content); Split the length of the letter for (String text:texts) {manager.sendtextmessage (number, NULL, text, NULL, NULL);//Send SMS}toast.maketext ( Mainactivity.this, R.string.success, Toast.length_long). Show (); } }}
Android Development Series (II): implementation of SMS Transmitter