Android Development Series (II): implementation of SMS Transmitter

Source: Internet
Author: User

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

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.