Android phone simulation application Applet

Source: Internet
Author: User

Android phone simulation application Applet

1. Introduction

There is already a call effect in android. To better understand android, We can compile a simple android program to simulate it.

Code Description

1. First, we will compile the view interface, which is reflected in the simplest way,

 


 

The visual interface is written through main. xml in layout. The Code is as follows:

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

 

<TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

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

 

<EditText

Android: id = "@ + id/phone"

Android: layout_width = "match_parent"

Android: layout_height = "wrap_content"

Android: EMS = "10">

 

<RequestFocus/>

</EditText>

 

 

<Button

Android: id = "@ + id/callBtn"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

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

 

</LinearLayout>

 

The above text is introduced in strings. xml in values:

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

<String name = "tipMsg"> enter the correct phone number </string>

<String name = "btn"> dialing </string>

 

Note: @ indicates reference, and if the code is absent, create

 

In the execution of main. when using xml files, you must first find the R. java internal class, and then find the strings under the corresponding values. xml file in strints. after an xml file is created, it is stored in the R. automatically Generated in java.

 

2. Trigger an event when you click dial-up

In this case, you need to get the button component in the Activity under src and get the information entered in the file box.

Package cn. csdn. hr. android. phone;

 

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;

 

Publicclass PhoneActivity extends Activity {

@ Override

Publicvoid onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

// Set the displayed view

SetContentView (R. layout. main );

 

// Obtain the current object

Button callBtn = (Button) findViewById (R. id. callBtn );

 

// The Anonymous class is used.

CallBtn. setOnClickListener (new OnClickListener (){

 

Publicvoid onClick (View v ){

// Trigger the button operation

 

// Obtain the Text Component www.2cto.com

EditText phone = (EditText) findViewById (R. id. phone );

 

// Intention to tell the component what to do when dialing

Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel :"

+ Phone. getText ()));

 

// Start processing intent

PhoneActivity. this. startActivity (intent );

 

// Give the call permission and set the permission in androidManfest

}

});

}

}

In the last step, assign Permissions to the User. To open AndroidManifest. xml, switch to the Permissions view, and click Add. On the page that appears, select User Permission:
 
Then select android. permission. CALL_PHONE from the drop-down list box in Name on the right. The final result is:
 
In this way, a simple telephone communication is complete. We can open two simulators for testing. For example, if one simulator is named 5554 and the other is named 5556, We can enter 5554 in the text box of 5556 to test.
Note:
Get Text Information phone. getText ()
 



From Han Xiaoshuang's column
 

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.