Android Development Phone Dialer Example detailed _android

Source: Internet
Author: User
Tags gettext stub

This example analyzes the use of Phone Dialer developed by Android. Share to everyone for your reference, specific as follows:

1. Create a new Android project called Javacallphone, and build a Callphoneactivity.java class under Cn.csdn.hr.activity package

2. Open the layout under the Res main.xml layout, set the layout of the horizontal layout, and then from the left to drag into the Textview,text files under the Phone, and button buttons , through the new The following string effect is as follows:

3. Open Callphoneactivity.java to write , the specific code is as follows:

Package cn.csdn.hr.activity;
Import android.app.Activity;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.text.Editable;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;  The public class Callphoneactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the './@Override Public
    void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Sets the View interface displayed Setcontentview (R.layout.main);
    Gets the corresponding component Button callbtn = (button) Findviewbyid (R.ID.CALLBTN) in the layout file;
Register for button click event Callbtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//dial//Get input Box editable object
EditText EditText = (edittext) Findviewbyid (R.id.editcallphone);
Gets the phone number entered Editable Editable = Edittext.gettext ();
Convert to String: editable.tostring ();
Intent Intent Intent = new Intent (Intent.action_call,uri.parse ("Tel:" +editable.tostring ()));
Implementation of startactivity (intent);
}
});

 }
}

4. Open the project manifest file andriod manifest.xml, select permissions, add the user call rights , the following figure:

5, in the source code to appear:

Copy Code code as follows:
<uses-permission android:name= "Android.permission.CALL_PHONE"/>

6, the last run as an Android application, to achieve dial-up function

Add: Android Phone Dialer Development notes

Androidactivity:

Package itcast.com;
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;
 public class Androidactivity extends activity {private EditText Editex;
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 To get button buttons = (button) This.findviewbyid (R.id.bt_number);
 Editex = (edittext) This.findviewbyid (R.id.et_number);  Set button listener//Mode two: via Anonymous inner class////Button.setonclicklistener (new Onclicklistener () {///@Override//public void OnClick (View v) {////TODO auto-generated Method Stub//String number = Editex.gettext (). toString ();//Get phone number//I Ntent Intent = new intent (); Create Intent//intent.setaction (Intent.action_call); Set the intent of the call//Intent.setdata (Uri.parse ("Telphone" + number)); Specific number to dial//startActivity (intent);//Activation Intent//}//}); Button.setonclicklistener (this);
 Current implementation This is androidactivity} public void Btlisten (View v) {String number = Editex.gettext (). toString ();
 Intent Intent = new Intent ();
 Intent.setaction (Intent.action_call);
 Intent.setdata (Uri.parse (number));
 StartActivity (Intent); ////mode three implemented, Onclicklistener//@Override//public void OnClick (View v) {////TODO auto-generated method Stub/
 /switch (V.getid ()) {//Case R.id.bt_number://The corresponding button time//String number = Editex.gettext (). toString ();
 Intent Intent = new Intent ();
 Intent.setaction (Intent.action_call);
 Intent.setdata (Uri.parse (number));
 StartActivity (Intent);
 Break  ////Mode one//class Myonclicklistener implements Onclicklistener {////@Override//public void OnClick (View V) {///TODO auto-generated Method Stub///Get text field//EditText Editext = (edittext) androidactivity.this//. Find
 Viewbyid (R.id.et_number); Get phone number//String number = Editext.gettext (). toString (); Intent Intent = new Intent (); Create Intent. Represents the action to be performed//intent.setaction (Intent.action_call);

 Intent to dial//Intent.setdata (Uri.parse (number));//dialing numbers such as A//startactivity (intent);//Activation Intent//}//}}

Mian.xml:

<?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:o" rientation= "vertical" >
  <textview
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:text=" @string/input_number "/>
  <edittext android:id=" @+id/et_number "
    android:layout_width= "fill_parent"
    android:layout_height= "wrap_content"
    android:hint= "@string Print "
    android:lines=" 1 "
    android:numeric=" integer "/>
  <button
    android:id=" @+id/bt_ Number "
    android:layout_width=" fill_parent "
    android:layout_height=" wrap_content "
    android:text=" @ String/bt_call "
    android:onclick=" Btlisten "/>
</LinearLayout>

Strings.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
  <string name= "Hello" >hello world, androidactivity!</string>
  <string name= "app_name" >AndroidCall</string>
  <string Name = "Input_number" > Please enter the phone number </string>
  <string name= "print" > Please enter the number </string>
  <string Name= "Bt_call" > Call </string>
</resources>

Requirements :

The procedure for dialing a call

Steps

1. configuration file

Strings.xml is responsible for the page is the text

Main.xml is responsible for the layout of the page and the ID of each space

2.java of code:

Get button by ID

Monitor button (parameter view)

3.MyOnCliockListenr

Handling Events:
Get editext, get phone number
Create a reference to the intent: Intent intrnt = new Intent ();
Set the action of the Intent object Setaction ()
Dial phone number SetData ()
Open Intent Statachivate (Intent)
To modify security exceptions: Permissions-->android.permission.call_phone in Androidmainfest.xml

I hope this article will help you with the Android program.

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.