Android Lesson 2: Android sends text messages

Source: Internet
Author: User
Tags cell phone icon

Address: http://digdeeply.info/archives/0404841.html

1. Create an android Project

Project name: sendmessage

Buildtarget: android2.2

Application name: send SMS

Package name: COM. SMS. Activity

Create activity: sendmessage

Min SDK version: 8

Ii. Editing Projects

1. Edit the strings. xml file as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> enter the mobile phone number: </string> <string name = "app_name"> send SMS </string> <string name = "content"> enter the information content: </string> <string name = "send"> send </string> </resources>

2. Edit the layout file main. XML with the following content:

<? 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"> <! -- Enter the mobile phone number tag --> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <! -- Mobile phone number editing box --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/mobile"/> <! -- Enter the information content tag --> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/content"/> <! -- Information Content edit box --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: minlines = "3" Android: id = "@ + ID/message"/> <! -- Send button --> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/Send" Android: id = "@ + ID/Send"/> </linearlayout>

 

Note that the Android: Id attribute is added to the phone number input box and the call button. For example, Android: Id = "@ + ID/mobile" in the phone number input box, @ code R. java, + ID code adds the static internal class of the ID. Mobile represents adding a constant member to the ID class. The ADT will automatically generate a constant value for us.

Android: minlines sets the minimum number of lines in the information content editing box.

3. Edit the call. Java content of the Java file:

Package COM. SMS. activity; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. telephony. smsmanager; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. util. log; public class sendmessage extends activity {/** called when the activity is first created. * // @ overridepublic void on Create (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the button according to the ID. Button button = (button) This. findviewbyid (R. id. send); // The register button is clicked the event button. setonclicklistener (New onclicklistener () {public void onclick (view v) {// obtain the mobile phone number edit box edittext mobiletext = (edittext) findviewbyid (R. id. mobile); // get the mobile phone number string mobile = mobiletext. gettext (). tostring (); // obtain information based on the ID Edit box edittext messagetext = (edittext) findviewbyid (R. id. message); // string message = messagetext. gettext (). tostring (); // The byte data that the mobile operator allows to send each time is limited. We can use the SMS tool provided by Android. If (message! = NULL) {smsmanager SMS = smsmanager. getdefault (); // If the SMS does not exceed the limit length, a list with the same length is returned. List <string> texts = SMS. dividemessage (Message); For (string text: texts) {SMS. sendtextmessage (mobile, null, text, null, null); log. I ("SMS", "Send a message ");}}}});}}

 

SMS. sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent ):

Destinationaddress: the recipient's mobile phone number

Scaddress: SMS center number, which can be left blank during testing.

Text: Information Content

Sentintent: whether the receipt is sent successfully. It will be detailed later.

Deliveryintent: whether the receipt is successful. It will be detailed later.

Log. I (): Write logs to facilitate debugging.

4. Edit androidmanifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. changcheng. activity "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". sendmessage "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. inte NT. Category. launcher "/> </intent-filter> </activity> </Application> <uses-SDK Android: minsdkversion =" 8 "/> <! -- Register the permission to send text messages, which must be added; otherwise, you cannot send text messages --> <uses-Permission Android: Name = "android. Permission. send_sms"/> </manifest>

 

If you do not have the permission to register and send SMS messages, you cannot use the system's SMS sending function. In future application development, if you have used system functions, you must register them in this file. We can view the functions of the android help manual. (... /Android-SDK-Windows/docs/reference/Android/manifest.permission.html)

3. Start the simulator

Who should we send a text message? We can start two simulators. Use one simulator to send messages to another simulator. First, use the cell phone icon on the toolbar to add an android2.1 simulator and enter another name.

Before starting two simulators, we need the simulator to "receive signals ". If our machine is connected to the Internet, after the simulator is started, a 3G sign is displayed next to the signal strength on the main interface. This indicates that the simulator has received the signal. If our machine cannot be connected to the Internet, set the IP address, gateway, and DNS server to the same value, for example, 192.168.0.100. If your machine is on a LAN but not connected to the Internet, set your gateway and DNS to the IP address of the route. Generally, the IP address of the route is 192.168.0.1.

OK. Now let's start two Simulators!

4. send text messages

After starting the simulator, we can see that the title bar of the simulator window contains 5554 and 5556. This is the port listened to by the simulator -- 127.0.0.1: 5554.

Right-click the project, run as Android Application, and select a simulator. For example, a simulator with port 5554 is selected. OK. The program is loaded into the simulator and runs automatically.

In the phone number editing box, enter 5556 (the port number of the receiver simulator) and click send!

OK. Have you seen the effect? 5556 on the main interface, a new text message is displayed next to the signal strength.

5. Use the ADT plug-in to send text messages to the simulator

If the machine is too slow to start two simulators, we can start only one simulator. Choose Windows> show View> Other> Android> emulator control to open the emulator control panel.

In the Telephony actions group box, voice is a call, and SMS is a text message. Incoming number is the port number of the simulator. We can also use this function to call or send text messages to our simulator.

 

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.