Java Learning Lesson 11th, Android to make phone calls function

Source: Internet
Author: User

1. First layout interface, the interface is a linear vertical way to layout

Activity_main.xml in the Layout interface file

<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= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/mobile"/> <EditText android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "@null"Android:id= "@+id/mobile"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button"Android:id= "@+id/button"//This is the ID of the import button, which is automatically generated in the R.java/></linearlayout>

2. In the mainactivity file,

 PackageCom.example.phone;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //Register button event when window contents are displayedButton button = (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewButtonclicklistener ()); }        //Defining internal Events    Private Final classButtonclicklistenerImplementsview.onclicklistener{@Override Public voidOnClick (View v) {//first get the Input box object, then take the input valueEditText Mobiletext =(EditText) Findviewbyid (r.id.mobile); String Number=Mobiletext.gettext (). toString (); //Define an Intent object, specify the behavior and data, call the Android dialing functionIntent Intent =NewIntent (); Intent.setaction ("Android.intent.action.CALL"); //intent.addcategory ("Android.intent.category.DEFAULT");//The method automatically adds categories to the intent, so you do not have to addIntent.setdata (Uri.parse ("Tel:" +Number )); //Here the phone number should be in URI format startactivity (intent); //The method automatically adds a category to the intent, so comment out the Android.intent.category.DEFAULT//Note here that this is called, and you need to declare the dialing permissions in the configuration file.//Declare in Androidmanifest.xml//<!--here to import permissions, otherwise you can't call--//<uses-permission android:name= "Android.permission.CALL_PHONE"/>        }            }    }

3. Declaring permissions in Androidmanifest.xml

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

This dialer, after experiment, installed on the phone, you can call directly, good!!!

Java Learning Lesson 11th, Android to make phone calls function

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.