Android Basic Development Small case SMS transmitter _android

Source: Internet
Author: User
Tags gettext

First look at the effect chart:

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 "Androi" 
    d:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "17DP" android:text= "Please enter your mobile number:" Android:textsize= "20DP"/> <edittext and 
  Roid:id= "@+id/edittext1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"  android:layout_below= "@+id/textview1" android:layout_centerhorizontal= "true" android:layout_margintop= "25DP" android:background= "@android:d rawable/editbox_dropdown_light_frame" android:ems= "android:inputtype=" Phon E "android:singleline=" true "> <requestfocus/> </EditText> <textview android:i D= "@+id/textview2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ alignleft= "@+id/textview1" android:layout_below= "@+id/edittext1" android:layout_margintop= "39DP" Android:te xt= "Please enter the contents of the message:" android:textsize= "20DP"/> <edittext android:id= "@+id/edittext2" Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/textview2" Android:layo Ut_centerhorizontal= "true" android:layout_margintop= "49DP" android:background= "@android:d Rawable/editbox_backgro und "android:ems=" 10 "android:hint=" Sent content ... "android:inputtype=" Textmultiline "android:lines=" 5 "/> <button A Ndroid:id= "@+id/btn_send" android:layout_width= "50DP" android:layout_height= "30DP" android:layout_below= "@+" Id/edittext2 "android:layout_centerhorizontal=" true "android:layout_margintop=" 30DP "android:background=" @d  Rawable/reply_send_button "/> </RelativeLayout></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; 
    @Override protected 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); } @Override Public void OnClick (View v) {switch (V.getid ()) {case R.id.btn_send://Get mobile number String = Et_numb 
      Er.gettext (). toString (). Trim (); 
      Get SMS Contents String content = Et_content.gettext (). toString (). Trim (); Determine if the contents of the cell phone and SMS are empty if (Textutils.isempty) | | Textutils.isempty (number)) {Toast.maketext (Mainactivity.this, "Mobile or SMS content is empty ...", Toast.length_lon 
        G). Show (); 
      Return 
        else {Smsmanager Smsmanger = Smsmanager.getdefault (); 
        Split the text into pieces, to prevent SMS content too long, send failure arraylist<string> contents = smsmanger.dividemessage (content);  Traversal SMS content for (String str:contents) {/* Smsmanger.sendtextmessage (destinationaddress, Scaddress, text, sentintent, deliveryintent) * Sendtextmessage method * The first parameter is the receiver of the message * second A parameter is where the text message comes from, and currently does not support filling in NULL can be * The third parameter text message send the content * The fourth parameter is to determine whether the message sent success * Fifth parameter is oppositeReceived a message from your text report/Smsmanger. Sendtextmessage (number, NULL, content, NULL, NULL); 
        Toast.maketext (Mainactivity.this, "Send success ...", Toast.length_long). Show (); 
    }} break;  }} </span>

Above is the Android SMS transmitter implementation code, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.