Android Basics-Phone Dialer

Source: Internet
Author: User

1. layout file:

Activity_main.xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.itheima.callphone.MainActivity" >    <EditTextAndroid:id= "@+id/et_number"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparentright= "true"Android:layout_alignparenttop= "true"Android:ems= "Ten" >        <Requestfocus/>    </EditText>    <ButtonAndroid:id= "@+id/bt_dial"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:layout_below= "@+id/et_number"Android:text= "@string/_dial" /></Relativelayout>

2.mainactivity.java

 PackageCom.itheima.callphone;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;Importandroid.text.TextUtils;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActionbaractivityImplementsOnclicklistener {//text box for entering a phone number    PrivateEditText Et_number; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //get the Number text entry boxEt_number =(EditText) Findviewbyid (R.id.et_number); //Get dial-up buttonButton bt_dial =(Button) Findviewbyid (r.id.bt_dial); Bt_dial.setonclicklistener ( This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }    //implement click events for various controls@Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt_dial:callphone ();  Break; default:             Break; }    }    //Implementing dial-up operations    Private voidCallphone () {//get the number of the Number entry boxString number =Et_number.gettext (). toString (). Trim (); //make a non-null judgment        if(Textutils.isempty (number)) {//Tips for usersToast.maketext (mainactivity. This, "Number cannot be empty", Toast.length_long). Show (); }        //Turn on Dial-up serviceIntent Intent =NewIntent ();        Intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" +Number ));    StartActivity (Intent); }}

3. Common errors

Error Description:

Java.lang.SecurityException:Permission denial:starting Intent {act=android.intent.action.call dat=tel:xxx-xxx-xxxx cmp=com.android.phone/. Outgoingcallbroadcaster} from Processrecord{b76dbe10 1322:com.itheima.callphone/10032} (pid=1322, uid=10032) Requires Android.permission.CALL_PHONE

Cause: No dial-up permissions were added to the application, such as the Add method.

Android Basics-Phone Dialer

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.