Android dialing device

Source: Internet
Author: User

During Development in the android simulator, you sometimes need to simulate the call function. Because the simulator cannot be directly used as a real machine, we need to simulate a real machine call in the simulator, first, you need to create two simulators for use as two Android phones. Because the Android system already has a call activity, you only need to write code to call it. The details are as follows:

1. Create the following layout:

    

Corresponding layout file XML:

    

    

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5  6     <TextView 7         android:id="@+id/textView1" 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content"10         android:text="@string/phone_tl"11         tools:context=".MainActivity" />12 13     <EditText14         android:id="@+id/editText1"15         android:layout_width="fill_parent"16         android:layout_height="wrap_content"17         android:layout_alignParentLeft="true"18         android:layout_below="@+id/textView1"19         android:layout_marginTop="14dp"20         android:ems="10" >21 22         <requestFocus />23     </EditText>24 25     <Button26         android:layout_width="wrap_content"27         android:layout_height="wrap_content"28         android:layout_alignParentLeft="true"29         android:layout_below="@+id/editText1"30         android:id="@+id/btn_call"31         android:text="@string/button" />32 33 </RelativeLayout>

2. Compile the code file:

    

1 package COM. example. phone; 2 3 Import android.net. uri; 4 Import android. OS. bundle; 5 import android. app. activity; 6 Import android. content. intent; 7 Import android. view. menu; 8 Import android. view. view; 9 Import android. widget. button; 10 Import android. widget. edittext; 11 12/** 13 * @ author fanchangfa14 * simulate a call by phone 15 */16 public class mainactivity extends activity {17 18 private edittext phone_text; // get the text box Object 19 private button btn_call used to enter the phone number; // get the call button object 20 @ override21 public void oncreate (bundle savedinstancestate) {22 super. oncreate (savedinstancestate); 23 setcontentview (R. layout. activity_main); 24 25/* 26 * initialize control 27 **/28 phone_text = (edittext) This. findviewbyid (R. id. edittext1); 29 30 btn_call = (button) This. findviewbyid (R. id. btn_call); 31 32 btn_call.setonclicklistener (New btn_listener (); 33} 34 35 // call the call button and click event: 36 private final class btn_listener implements view. onclicklistener {37 public void onclick (view v) 38 {39 string phone = phone_text.gettext (). tostring (); 40 41 intent int_call = new intent (); 42 43 int_call.setaction ("android. intent. action. call "); 44 int_call.setdata (URI. parse ("Tel:" + phone); 45 46 // when using intent, you also need to set its category. However, 47 // The method automatically adds category: Android. intent. category. default48 49 startactivity (int_call); 50} 51} 52 53 @ override54 public Boolean oncreateoptionsmenu (menu) {55 getmenuinflater (). inflate (R. menu. activity_main, menu); 56 return true; 57} 58}

 

3. the main task is completed, but the function cannot be implemented smoothly at this time. In order to protect users' private information, Google has set some permissions. during development, we need to add specific permissions for normal use, in addition, we need to add the dialing permission to androidmainfest. XML file:

  

1 <! -- Add the call permission --> 2 <uses-Permission Android: Name = "android. Permission. call_phone"/>

4. Run the program at this time, but two simulators are required. Start two simulators:

    

    

    

 

We can see that the program we wrote is deployed on the simulator 5556 and another simulator is 5554. Now, in our program, enter 5554 and click the call button, the system call activity is automatically called. The effect is as follows:

    

 

This function is usually used when the program needs to call the call program. For example, when developers view the personnel information, if there is a phone number, they can directly call the activity of this system to call the call.

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.