Last time, we made a simple hello world. This time, we wrote a telephone call system. In fact, this call is not really called by our own program, instead, call the system program.
First, create the project Phone and create the required string under the project's alues à strings.
<String name = "app_name"> Phone </string>
<String name = "input_info"> enter the number </string>
<String name = "dial_caotion"> call </string>
Create an interface in layout à main. xml
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/input_info"/>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/phone_number"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/dial_btn"
Android: text = "@ string/dial_caotion"
/>
This program uses a total of input boxes and buttons, and the text will not be repeated. The rest is our java code.
We need to write the implementation code in src à PhoneActvity. First, we should first obtain the phone number entered by the user in the input box, and then write a listener internal class to listen to button events, the specific implementation code is as follows:
NumberEt = (EditText) this. findViewById (R. id. phone_number );
DialBtn = (Button) this. findViewById (R. id. dial_btn );
DialBtn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Call the dialing service of the system to enable the dialing Function
String phone_number = numberEt. getText (). toString ();
Phone_numberphone_number = phone_number.trim ();
If (phone_number! = Null &&! Phone_number.equals ("")){
// Encapsulate a call intent and package the phone number into a Uri object
Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + phone_number ));
PhoneActivity. this. startActivity (intent );
}
}
});
From chenglong0513