Android Development Series (i): the implementation of telephone dialer

Source: Internet
Author: User

The goal is to achieve a simple phone dialing function.


First, you can see several controls on top, with a few words on top.

After we have established the project, we can first add these words in the strings.xml of the values directory under the Res directory, in order to achieve a better internationalization.

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "Hello" >hello world, mainactivity!</string>    <string name= "app_name" > Phone dialing device </string>    <string name= "mobile" > Please enter your phone number </string>    <string name= "button" > Dialing </string></resources>
Next, we need to implement this activity window. As you can see, there are three components in this interface: the first line of "Please enter the phone number", the second line of the phone number text box, the third line of the "Dial" button.

Therefore, we add these components in the Main.xml configuration file of the layout directory in the Res directory:

       <textview      android:layout_width= "fill_parent"     android:layout_height= "wrap_content"     android:text= "@string/mobile"    /><edittextandroid:layout_width= "fill_parent"     android:layout_height= "Wrap_ Content "    android:id=" @+id/mobile "    /><buttonandroid:layout_width=" Wrap_content "     android: layout_height= "Wrap_content"    android:text= "@string/button"    android:id= "@+id/button"    />
When this is done, the interface is already done.


At this point, we need to implement the dialer dialing function via Java:

We need to write Mainactivity.java:

Package Cn.itcast.phone;import Android.app.activity;import Android.content.intent;import android.net.Uri;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.EditText;public        Class Mainactivity extends Activity {private EditText mobiletext;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Mobiletext = (EditText) Findviewbyid (r.id.mobile);//Get text input box: The property with the ID of mobile in Main, button button = (Button) this.find Viewbyid (R.id.button); Get button Button.setonclicklistener (new Buttonclicklistener ());//Set the listener for the Click event} private Final class Buttoncli Cklistener implements View.onclicklistener{public void OnClick (View v) {//default implementation method, when the button is clicked it is called string number = Mobiletext.gettext (). toString (); Use the text input box to get the value of the text input box Intent intent = new intent (); Get an Intent Object Intent.setaction ("Android.intent.action.CALL"); Pass the action name Intent.setdata (Uri.parse ("Tel: "+ number"); Pass Data startactivity (intent);//The method will automatically add a category for intent: Android.intent.category.DEFAULT}}}

Let's analyze the process of the following procedure:

First, call Setcontentview (R.layout.main) to get the main window, and then Findviewbyid (r.id.mobile) to get the text input box with ID mobile in Main.xml.

Next, pass Findviewbyid (R.id.button) and get the button with ID button in Main.xml.

Continue, use Setonclicklistener (New Buttonclicklistener ()) to set the listener for the Click event.


Again, we need to write a buttonclicklistener () class that is marked with final to not be inherited.

Add the non-implemented method to this class, which is called when clicked: Onckick (View v).

In the method, call Mobiletext.gettext (). toString () to get the phone number entered by the text box.

Then, the action name of the call is delivered via intent: "Android.intent.action.CALL"

Passing Data via intent: Intent.setdate (Uri.parse ("Tel:" +number));

Next Open the intent intent.


Since making a call involves the privacy of the data, we need to add a security control to the Androidmainfest.xml:

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

By this time, a telephone dialer was basically finished.




Android Development Series (i): the implementation of telephone dialer

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.