Android development path (3)-analog call implementation

Source: Internet
Author: User

Android development path (3) --- simulate the implementation of phone calls

A ndroid development is A very interesting thing. Simulating A call is A very interesting thing. Your own virtual machine calls your own virtual machine. The following describes how to simulate a call.

First, create an Android Project Phone

You should first define the String and value in the String. xml file in values.

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<String name = "input_info"> enter the number you want to call </string> // define the number you want to call.

<String name = "button_caption"> call </string> // a button to call

</Resources>

Then, in the main. xml file in the Layout folder, set the code in the interface

<? 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 = "wrap_content"

Android: baselineAligned = "false"

Android: orientation = "vertical">

<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: text = "@ string/button_caption"

Android: id = "@ + id/dial_btn"

/>

</LinearLayout>

The interface is defined, and the generated interface is

The next step is to learn the source code of our program. The code written in Phone is

Package cn. csdn. PhoneActivity;

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 class PhoneActivity extends Activity {

/** Called when the activity is first created .*/

EditText numberEt;

Button dialBtn;

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

FindViews ();

DialBtn. setOnClickListener (myListener );

}

Private void findViews (){

// TODO Auto-generated method stub

NumberEt = (EditText) this. findViewById (R. id. phone_number );

DialBtn = (Button) this. findViewById (R. id. dial_btn );

}

Private OnClickListener myListener = new Button. OnClickListener (){

Public void onClick (View v ){

// Call the system's dialing service to enable the dialing 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 call number into a Uri object to pass in

Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + phone_number ));

PhoneActivity. this. startActivity (intent );

}

}

};

}

Finally, you must add user constraints to the AndroidManifest. xml file so that you can make a call. In this case, you must select Uses Permission to add the android. permission. CALL_PHONE method.

In this way, the call function can be realized.

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.