Simple app for Android to call and send text messages

Source: Internet
Author: User

Call and send text messages on the simulator. You can also use the emulator control of the ADT plug-in of eclipse on the command line. The specific method is as follows.

 

Command line:

Assume that the port number of the simulator you are currently enabling (on the title bar of the simulator) is 5554.

1. Open the command line and enter Telnet localhost 5554 and press Enter. The following prompt is displayed, indicating that you have successfully connected to port 5554.

 

Android Console: Type 'help' for a list of commands

OK

2. Enter GSM call 123456 and press enter to call the phone number 123456. Later, you will see the interface on the simulator that is calling the phone number.

 

Emulator control:

In eclipse, choose windows> show View> Other> Android> emulator control to open the emulator control panel.

In telephony actions, first enter the phone number to be called, then select voice or SMS (if the simulator port number is 5554, enter 5554), and click call to make the call, the caller interface is displayed on the simulator.

 

 

Next, let's go to the topic. We use a program to simulate the call and text message functions. Source code

1. Create a voiceactivity and the layout file main. xml. You can call or send a text message. If you make a call, you can call it directly. If you send a text message, you will jump to the smsactivity. The Code is as follows.

Voiceactivity:

 

Public class voiceactivity extends activity {<br/> private edittext inputedittext; <br/> private button voicebtn; <br/> private button smsbtn; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> inputedittext = (edittext) findviewbyid (R. id. inputnumedittext); <br/> voicebtn = (button) findviewbyid (R. id. Voicebtn); <br/> smsbtn = (button) findviewbyid (R. id. smsbtn); </P> <p> // response of the voice button <br/> voicebtn. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> string phonenum = inputedittext. gettext (). tostring (); <br/> // determines whether the input is null <br/> If (phonenum = NULL | "". equals (phonenum) {<br/> toast. maketext (voiceactivity. this, <br/> "Please input phonenum! ", Toast. length_short) <br/>. show (); <br/> return; <br/>}< br/> // call <br/> intent = new intent (intent. action_call, Uri. parse ("Tel:" <br/> + phonenum); <br/> startactivity (intent); <br/>}< br/> }); </P> <p> // SMS button response <br/> smsbtn. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> string phonenum = inputedittext. gettext (). tostring (); <br/> If (Phone Num = NULL | "". Equals (phonenum) {<br/> toast. maketext (voiceactivity. This, <br/> "Please input phonenum! ", Toast. length_short) <br/>. show (); <br/> return; <br/>}< br/> // call <br/> intent = new intent (); <br/> intent. setclass (voiceactivity. this, smsactivity. class); <br/> // bind the phone number data to the smsactivity <br/> bundle mbundle = new bundle (); <br/> mbundle. putstring ("phonenum", phonenum); <br/> intent. putextras (mbundle); </P> <p> startactivity (intent); <br/>}< br/>}); <br/>}< br/>}

Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: TEXT = "@ string/inputprompt"/> <br/> <edittext Android: Id = "@ + ID/inputnumedittext" <br/> Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <br/> <linearlayout Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: orientation = "horizontal"> <br/> <button Android: Id = "@ + ID/voicebtn" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: text = "@ string/Voice"/> <br/> <button Android: Id = "@ + ID/smsbtn" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: TEXT = "@ string/SMS"/> <br/> </linearlayout>

2. The following is the smsactivity text message and the layout file SMS. xml. The Code is as follows.

Smsactivity:

Public class smsactivity extends activity {<br/> private edittext messageedittext; <br/> private button sendbtn; <br/> private button clearbtn; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. SMS); <br/> messageedittext = (edittext) findviewbyid (R. id. messageedittext); <br/> sendbtn = (button) findviewbyid (R. Id. sendbtn); <br/> clearbtn = (button) findviewbyid (R. id. clearbtn); </P> <p> sendbtn. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // receive phone number data <br/> bundle = getintent (). getextras (); <br/> string phonenum = bundle. getstring ("phonenum"); <br/> // obtain the sent content <br/> string message = messageedittext. gettext (). tostring (); <br/> If (phonenum = NULL | "". EQ Uals (phonenum) {<br/> toast. maketext (smsactivity. This, <br/> "Please input SMS content! ", Toast. length_short) <br/>. show (); <br/> return; <br/>}< br/> // send a text message <br/> sendsms (phonenum, message ); <br/>}< br/>}); </P> <p> // empty message input box <br/> clearbtn. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> messageedittext. settext (""); <br/>}< br/>}); <br/>}</P> <p> private void sendsms (string phonenum, string message) {<br/> // initialize the smsmanager class for sending text messages <br/> smsm Anager smsmanager = smsmanager. getdefault (); <br/> pendingintent Pi = pendingintent. getactivity (this, 0, new intent (this, <br/> voiceactivity. class), 0); <br/> // if the text message length exceeds 70, it is divided into several messages <br/> If (message. length ()> 70) {<br/> arraylist <string> msgs = smsmanager. dividemessage (Message); <br/> for (string MSG: MSGs) {<br/> smsmanager. sendtextmessage (phonenum, null, MSG, PI, null); <br/>}< br/>} else {<br/> S Msmanager. sendtextmessage (phonenum, null, message, PI, null); <br/>}< br/> toast. maketext (this, "Send message success! ", Toast. length_short) <br/>. Show (); <br/>}< br/>}

SMS. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: TEXT = "@ string/inputmessage"/> <br/> <edittext Android: Id = "@ + ID/messageedittext" <br/> Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" <br/> Android: minlines = "3"/> <br/> <linearlayout Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <br/> <button Android: Id = "@ + ID/sendbtn" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: text = "@ string/Send"/> <br/> <button Android: id = "@ + ID/clearbtn" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: TEXT = "@ string/clear"/> <br/> </linearlayout>

3. The androidmanifest. xml and strings. xml file codes are attached here:

Androidmanifest. xml :( note that you must add the permission to call and send text messages)

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. byread "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <br/> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". voiceactivity "Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <cat Egory Android: Name = "android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: Name = ". smsactivity "> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "4"/> <br/> <! -- Note: here you need to add the permission to call and send text messages to the project --> <br/> <uses-Permission Android: Name = "android. permission. call_phone "/> <br/> <uses-Permission Android: Name =" android. permission. send_sms "/> <br/> </manifest>

Strings. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <string name = "app_name"> Android voice </string> <br/> <string name = "inputprompt"> Please input phonenum: </string> <br/> <string name = "inputmessage"> Please input SMS content: </string> <br/> <string name = "Voice"> voice </string> <br/> <string name = "SMS"> SMS </string> <br /> <string name = "send"> send </string> <br/> <string name = "clear"> clear </string> <br/> </resources>

4. it is done. If you enable two simulators, for example, one port number is 5554, the other is 5556, and the running project is in the 5556 port number simulator. Enter 5554 in the phone number input box, after you click to call or send a text message, the 5554 simulator will receive an incoming call or text message.

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.