android--Simple Texting function

Source: Internet
Author: User

First, prepare the character resource

<string name= "Tip_phone" > Please enter your phone number </string>

<string name= "Tip_sms" > Please enter send Message </string>

<string name= "Tip_send" > Send </string>

Second, the page layout

<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"

tools:context= "${relativepackage}.${activityclass}" >

<textview

Android:id= "@+id/tvphone"

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content"

android:text= "@string/tip_phone"/>

<edittext

Android:id= "@+id/etphone"

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content"

android:layout_below= "@+id/tvphone"

Android:inputtype= "Phone"/>

<textview

Android:id= "@+id/tvsms"

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content"

android:layout_below= "@+id/etphone"

android:text= "@string/tip_sms"/>

<edittext

Android:id= "@+id/etsms"

Android:layout_width= "Match_parent"

android:layout_height= "100DP"

android:layout_below= "@+id/tvsms"

Android:inputtype= "textmultiline"/><!-SMS content line-up

<button

Android:id= "@+id/sendbtn"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:layout_below= "@+id/etsms"

android:onclick= "Sendsms"

android:text= "@string/tip_send"/>

</RelativeLayout>

Third, the specific implementation of the Code

public class Smsactivity extends Activity {

Private EditText etphone,etsms;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (r.layout.activity_sms);

Gets the phone number of the control object

Etphone= (EditText) Findviewbyid (R.id.etphone);

Gets the control object that sent the message

Etsms= (EditText) Findviewbyid (r.id.etsms);

}

public void Sendsms (View v) {

Switch (V.getid ()) {

Case R.ID.SENDBTN:

String Phone=etphone.gettext (). toString ();

String Content=etsms.gettext (). toString ();

2. Message Manager Object

Smsmanager Smsmanager=smsmanager.getdefault ();

3. Split SMS Content

List<string> list=smsmanager.dividemessage (content);

4. Traverse the Sending message

for (String sms:list) {

5. Send a message

Smsmanager.sendtextmessage (phone, NULL, SMS, NULL, NULL);

}

6. Prompt text message sent successfully

Toast.maketext (This, "sent successfully", Toast.length_long). Show ();

Break

Default

Break

}

}

}

Iv. Obtaining permissions

Add text message permissions in Androidmanifest.xml

<uses-permissionandroid:name= "Android.permission.SEND_SMS"/>

The final effect is as follows:

android--Simple Texting function

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.