Phone dial for learning notes for Android Projects

Source: Internet
Author: User

Development Environment:

Win XP + eclipse-jee-helios (version 3.6) + ADT (version 10.0.1) + Android SDK (version 10 );

Test environment for simulators and real machines: Android2.2

1. Software Interface Design:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1014042091-0.png "title =" 1.png"/>


Figure 1

  • First, add the string required for layout to string. xml in phone-> res-> values. The string. xml Code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello World, MainActivity! </String> <string name = "app_name"> phone number generator </string> <string name = "mobile"> enter the mobile phone number </string> <string name = "button"> dial number </string> </resources>

Appears as a key-value pair, as shown in

<String name = "app_name"> phone number generator </string>

Define the id of the string "telephone dial" as"app_name"In the layout, the corresponding id value can be used to reference the string.

  • Add the layout code in main. xml in phone-> res-> layout. from figure 1, we can see that the layout is a vertical linear layout. The specific implementation code is as follows:

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/mobile"        />    <EditText        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/mobile"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button"        android:id="@+id/button"        /></LinearLayout>

2. Add a processing object for the click event to the dial button.

The specific implementation idea is as follows: first, find the dialing button in all controls based on the control id and set the listener. That is, when you click the dialing button, execute the corresponding dialing operation.

It should be noted that the code we write calls the dialing function provided by the Android mobile phone to determine which Activity provides the function? Activate this Activity so that it can call us. To activate this Activity, you need
The intent filter that comes with the Activity matches, and shows the permission to call Android in the list file ).

  • Add the code to implement the dialing button in MainActivity. java in phone-> src-> cn. hao. phone, and define the following global variables in the MainActivity class:

    private EditText mobileText;

    Add the following code to the onCreate () method:

    mobileText = (EditText) findViewById(R.id.mobile);Button button = (Button) this.findViewById(R.id.button);button.setOnClickListener(new ButtonClickListener());

    MethodButtonClickListener () in onCreate () generationThe Code is as follows:

    Private final class ButtonClickListener implements View. onClickListener {public void onClick (View v) {String number = mobileText. getText (). toString (); Intent intent = new Intent (); intent. setAction ("android. intent. action. CALL "); intent. setData (Uri. parse ("tel:" + number); startActivity (intent); // The Intent is automatically added to the method: android. intent. category. DEFAULT }}

3. display the call permission in the AndroidManifest. xml file.

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

Note: The permission declaration should be outside the <application> </application> label.

The running result is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1014045303-1.png "title =" 1.png"/>

The result of simulating a call using two simulators is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1014043b0-2.png "title =" 3.png"/>

Summary: The implementation of this program function is not entirely compiled by ourselves. If you are interested, you can check the source code of the dial-up function provided by the Android mobile phone at least tens of thousands ), instead, we matched the intention of the dial-up function provided by the Android mobile phone and presented the corresponding operation permissions.

This article from the "Flowers bloom" blog, please be sure to keep this source http://020618.blog.51cto.com/6098149/1292700

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.