*/What do we need to simulate a call program?
1. Draw the android UI
2. The interface is equipped
In our design on the UI, we found that there are two string strings, one text box and one button.
First add two strings to the res-values-strings.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, myphoneactivity! </String> <string name = "app_name"> Myphone </string> <string name = "input_info"> enter the mobile phone number </string> <string name = "button_caption"> dial out </string> </resources>
Then add the layout interface to the res-layout-main.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <! -- Add the textview component --> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/input_info"/> <! -- Add the edittext component --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/phone_number"/> <! -- Add the button --> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/button_caption" Android: id = "@ + ID/dial_btn"/> </linearlayout>
OK. The page is set up. What should I do next?
3.
Package CN. class3g. activity; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext;/*** key points for creating an activity: * 1. an activity is a class, and the class must inherit activity * 2. override oncreate Method * 3. every activity must be in androidmanifest. Configure in XML * 4. in Main. XML to add the necessary controls **/public class myphoneactivity extends activity {// find the edited controls edittext numberet; button dialbtn; // override the oncreate method @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); findviews (); dialbtn. setonclicklistener (mylistener);} // retrieve the control content private void findviews () {numberet = (edittext) This. findviewbyid (R. I D. phone_number); dialbtn = (button) This. findviewbyid (R. id. dial_btn);} private onclicklistener mylistener = new button. onclicklistener () {@ overridepublic void onclick (view v) {// call the system's dialing service to enable the call function string phone_number = numberet. gettext (). tostring (); phone_number = phone_number.trim (); If (phone_number! = NULL &&! Phone_number.equals ("") {// encapsulate the intent of a call and package the phone number into a URI object to pass in intent = new intent (intent. action_call, Uri. parse ("Tel:" + phone_number); myphoneactivity. this. startactivity (intent); // internal class }}};}
Many of you think that you can make a phone call? Actually, this is not because permissions are still involved. How do I set permissions?
To modify the permission in androidmanifest. XML, you can directly add <uses-Permission Android: Name = "android. Permission. call_phone"/>
You can also use
Open Editor
Edit in permission Editor
Select dialing permission
Save it.
Run as-Android Application
If you have a real machine, you can test it directly on the real machine. The test is correct.
If there is no real machine, you can enable two simulators. One is to install the program in the simulator, and the other is to call the simulator number.
The simulator number is 5556.
For example, you can see the Myphone Application
Click to run
Enter 5556 in the input box and click to dial
The implementation of the simulated call program is finished here!