Android phone call Program Implementation

Source: Internet
Author: User

 

Today, we use Android to write a small program that enables calls. First, create an Android project Phone and modify the String. xml in Values in Layout. The Code is as follows:

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

<Resources>

<String name = "hello"> Hello World, PhoneActivity! </String>

<String name = "app_name"> Phone </string>

 

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

<String name = "dial_caption"> call </string>

</Resources>

Then add several attributes to main. xml. The Code is as follows: www.2cto.com

<TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/input_into"/>

<EditText

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: id = "@ + id/phone_number"/>

<Button

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/dial_caption"

Android: id = "@ + id/dial_btn"/> t

Layout is the layout. After modifying the code of Main. xml, the following results will be displayed:

 

The following code is automatically generated in R. Java:

Public static final class drawable {

Public static final int ic_launcher = 0x7f020000;

}

Public static final class id {

Public static final int dial_btn = 0x7f050001;

Public static final int phone_number = 0x7f050000;

}

Public static final class string {

Public static final int app_name = 0x7f040001;

Public static final int dial_caption = 0x7f040003;

Public static final int hello = 0x7f040000;

Public static final int input_into = 0x7f040002;

}

The following code is available in the PhoneActivity. java file:

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;

Public class PhoneActivity extends Activity {

 

EditText numberEt;

Button dialBtn;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

FindViews ();

DialBtn. setOnClickListener (myListener );

}

 

Public void findViews (){

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 ){

String phone_number = numberEt. getText (). toString ();

Phone_number = phone_number.trim ();

If (phone_number! = Null &&! Phone_number.equals ("")){

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

PhoneActivity. this. startActivity (intent );

}

}

};

}

In AndroidManifest. xml, add:

<Uses-sdk android: minSdkVersion = "10"/>

<Uses-permission android: name = "android. permission. CALL_PHONE"/>

Save, the program like a phone call is finished, open the mobile phone, and right-click the project name, select Run As...-> Android Application to Run, you can call.

Note: The program needs to start two virtual phones to implement mutual dialing.

The effect after running is as follows:

 

From column LLLAZY520

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.