An Android basic development small case, SMS transmitter:
Code Demo diagram:
Layout file:
Activity_main.xml
<span style= "Font-family:comic Sans ms;font-size:14px;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_alignparenttop= "true" Android:layout_centerhorizontal= "Tru E "android:layout_margintop=" 17DP "android:text=" Please enter the mobile phone number: "android:textsize=" 20DP "/> <editt Ext android:id= "@+id/edittext1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content "android:layout_below=" @+id/textview1 "android:layout_centerhorizontal=" true "Android:la yout_margintop= "25DP" android:background= "@android:d rawable/editbox_dropdown_light_frame" android:ems= "10" Android:inputtype= "Phone" android:singleline= "true" > <requestfocus/> </EditText> <textview android:id= "@+id/textview2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/textview1" android:layout_below= "@+id/edittext1" Androi d:layout_margintop= "39DP" android:text= "Please enter the contents of the text message:" android:textsize= "20DP"/> <edittext Android Oid:id= "@+id/edittext2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android oid:layout_below= "@+id/textview2" android:layout_centerhorizontal= "true" android:layout_margintop= "49DP" Android:background= "@android:d rawable/editbox_background "android:ems=" "android:hint=" Send content ... "android:inputtype=" Tex Tmultiline "android:lines=" 5 "/> <button android:id=" @+id/btn_send "android:layout_width=" 50 DP "android:layout_height=" 30DP "android:layout_below=" @+id/edittext2 "Android:layout_centerhorizonta L= "true" android:layout_margintop= "30DP" android:background= "@drawable/reply_send_button"/></relative Layout></span>
Java code:
<span style= "Font-family:comic Sans ms;font-size:14px;" >package com.bzu.gxs;import java.util.arraylist;import Android.os.bundle;import android.app.Activity;import Android.telephony.smsmanager;import Android.text.textutils;import Android.view.menu;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity implements Onclicklistener {private EditText et_number; Private EditText et_content;private Button btn_send; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); et_number = (EditText) findViewById ( R.ID.EDITTEXT1); et_content = (EditText) Findviewbyid (r.id.edittext2); btn_send = (Button) Findviewbyid (r.id.btn_send) ; Btn_send.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.btn_send://Gets the phone number string # = Et_number.gettext (). ToString (). Trim ();//Get SMS Contents String content = Et_content.gettext (). toString (). Trim ();//Determine if the contents of the phone and text message are empty if ( Textutils.isempty (content) | | Textutils.isempty (number)) {Toast.maketext (Mainactivity.this, "mobile phone numbers or SMS content is empty ...", Toast.length_long). Show (); return;} else {Smsmanager Smsmanger = Smsmanager.getdefault ();//split SMS into multiple fragments to prevent SMS content from being too long, sending failed arraylist<string> contents = Smsmanger.dividemessage (content);//Traverse SMS content for (String str:contents) {/* * Smsmanger.sendtextmessage ( Destinationaddress, scaddress, text, sentintent, deliveryintent) * The first parameter of the Sendtextmessage method is the recipient of the message * The second argument is where the text message came from, Currently does not support fill in the null can be * The third parameter text message to send the content * The fourth parameter is to determine whether the text message is sent successfully * The fifth parameter is a message to the opposite received you sent the text report */smsmanger.sendtextmessage (number, NULL, Content, NULL, NULL); Toast.maketext (Mainactivity.this, "Send success ...", Toast.length_long). Show ();}} Break;}}} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basic development-SMS transmitter