Implementation of a simple phone call program for Android
Step 1: Interface Design
Step 2: overall implementation:
Phone call is the main function of mobile phones. The first program in Android is to implement the call function.
1. First, add the corresponding text information to strings. XML in the values directory.
<String name = "input_info"> enter the number you want to call: </string> <string name = "dial_caption"> call </string>
2. Implement the page layout in layout and introduce the corresponding text information.
<! -Insert the page title --> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/input_info"/> <! -Define the text box --> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/phone_number"/> <! -Define a button --> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/dial_btn" Android: TEXT = "@ string/dial_caption"/>
3. Write Java code:
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; Public classphoneactivity extends activity {// define the text box and button edittext numberet; button dialbtn; publicvoidoncreate (bundle savedinstancestate) {super. oncreate (savedinstancest Ate); setcontentview (R. layout. main); findviews (); dialbtn. setonclicklistener (New onclicklistener () {public void onclick (view v) {// call the dialing service of the system to implement the dialing function stringphone_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 intentintent = newintent (intent. action_call, Uri. parse ("Tel:" + phone_number); phoneactivity. this. startactivity (intent) ;}});} public void findviews () {numberet = (edittext) This. findviewbyid (R. id. phone_number); dialbtn = (button) This. findviewbyid (R. id. dial_btn );}}
4. Don't forget to grant it the permission to write in androidmanifest. xml.
<uses-permission android:name="android.permission.CALL_PHONE"/>
5. Start the simulator twice and simulate 5556 to call 5554.
Test in the android virtual machine as shown in the following figure:
The above is a simple call implementation. I hope you can understand it!