Android to achieve SMS delivery function _android

Source: Internet
Author: User
Tags gettext

The example of this article realizes the sending function of SMS between two simulator, share it to everybody for reference, realize the content as follows

1. The contents of the edited String.xml file are:

<?xml version= "1.0″encoding=" utf-8″?>
<resources> <string name= "App_name
" >sendmesage </string>
<string name= "action_settings" >Settings</string>
<string name= "Hello_" World ">hello world!</string>
<string name=" Hello "> Please enter your mobile number </string>
<string name=" Content ">please input your message</string>
<string name=" send ">Send</string>
</ Resources>

2. The contents of the editorial Main.xml are:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:orientation=" vertical "android:layout_width=" fill_parent "Android:layout_" height= "Fill_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen Activity_vertical_margin "tools:context=". SendMessage "> <textview android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:text" = "@string/hello"/> <edittext android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android
: id= "@+id/mobile"/> <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "@string/content"/> <edittext android:layout_width= "Fill_parent" android:layout_height= Content "android:minlines=" 3″android:id= "@+id/message"/> <button android:layout_Width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/send" android:id= "@+id/send"/>
 </LinearLayout>

3. Sendmessage.java

Import java.util.List;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Import Android.widget.EditText; public class SendMessage extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.
    OnCreate (savedinstancestate);

    Setcontentview (R.layout.activity_send_message);
    Button button = (button) This.findviewbyid (r.id.send);  Button.setonclicklistener (New Onclicklistener () {//Register button click event @Override public void OnClick (View arg0) {EditText Mobiletext = (edittext) Findviewbyid (r.id.mobile);//Get phone number String mobile

                = Mobiletext.gettext (). toString (); EditText MessageText = (edittext) Findviewbyid (r.id.message);//Get SMS content String message = Messagetext.gettext (

               ). ToString (); Mobile operators allow everyThe byte data sent is limited, you can use the SMS tool provided by Android.

                    if (message!= null) {Smsmanager SMS = Smsmanager.getdefault ();

                    List texts = sms.dividemessage (message);
                    for (String text:texts) {sms.sendtextmessage (mobile, NULL, text, NULL, NULL);
  }
                }
            }

    }); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Ba
    R if it is present.
    Getmenuinflater (). Inflate (r.menu.send_message, menu);
  return true;

 }

}

4. Edit Androidmanifest.xml content and add a single statement

<!– registration to send SMS permission –>
<uses-permission android:name= "Android.permission.SEND_SMS"/>

If you do not register this message to send the permission, will not use the system's SMS send function. In other cases that should be developed, there is a need to register in this file when using the system functionality.

5. Start two simulators

    • The first simulator, launched through Eclipse, runs the application to start an Android emulator. The ID is usually 5554.
    • The second simulator, which starts with a DOS command. CD to the SDK tools directory, using the command: EMULATOR-AVD "AVD name"

6. Send text messages using the ID of the simulator as a phone number

Effect Chart:

I hope this article will help you learn about Android software programming.

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.