Android implementation simple SMS transmitter

Source: Internet
Author: User

Layout:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height=        "Match_parent" tools:context= "com.example.msgSend.MainActivity" tools:ignore= "Mergerootframe" > <textview Android:id= "@+id/tv_pleaseinputphonenum" android:layout_width= "wrap_content" android:layout_height= "WR        Ap_content "android:text=" @string/pleaseinputphonenum "/> <edittext android:id=" @+id/et_phonenum " Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentlef T= "true" android:layout_below= "@+id/tv_pleaseinputphonenum" android:layout_margintop= "14DP" android:e ms= "android:inputtype=" Phone "/> <textview android:id=" @+id/et_pleaseinputcontent "Andro Id:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_below= "@+id/et_phoneNum" android:layout_margintop= "16DP" android:text= "@string/pleaseinputcontent"/> <edittext Andro Id:id= "@+id/et_content" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android oid:layout_below= "@+id/et_pleaseinputcontent" android:layout_margintop= "26DP" android:layout_marginright= "10    DP "android:layout_marginleft=" 10DP "android:ems=" android:lines= "5" > </EditText> <button android:id= "@+id/bt_send" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:layout_alignleft=" @+id/et_content "android:layout_below=" @+id/et_content "Android:la yout_margintop= "26DP" android:text= "@string/send"/></relativelayout>

  

Activity code:

Package Com.example.msgsend;import Java.util.list;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.telephony.smsmanager;import android.text.TextUtils; 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 Actionbaractivity implements onclicklistener{/** phone */private EditText etphonenum;/** SMS content */private EditText etcontent;/** send button */private button BtS end; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get Component Etphonenum = (EditText) Findviewbyid (r.id.et_phonenum); etcontent = (EditText) Findviewbyid (r.id.et_content); btsend = (Button) Findviewbyid (r.id.bt_send); System.out.println ("Get Success");//Register Click event Btsend.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.bt_send:sendmessage (); break;default:break;}} Implement SMS Send private void SendMessage () {String phonenum = Etphonenum.gettext (). toString (); String content = Etcontent.gettext (). toString ();//Toast Prompt if (textutils.isempty (phonenum) | | Textutils.isempty (content)) {Toast.maketext (this, "mobile phone number and text message cannot be empty", Toast.length_long). Show (); return;} Smsmanager Smsmanager = Smsmanager.getdefault ();//SMS is limited in length, the content is split directly list<string> contents = Smsmanager.dividemessage (content);//Send for (String content1:contents) {smsmanager.sendtextmessage (phonenum, NULL, Content1, NULL, NULL);}}}

  

Effect:

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.