Android apps are two dialing Methods: androidapp

Source: Internet
Author: User

Android apps are two dialing Methods: androidapp

What should I do if I want to add a dialing function to the APP? Android provides two methods: The ACTION_CALL method and the ACTION_DIAL method to open the dialing interface of the system.

Here is a small example.

First, you need to add a permission in AndroidManifest. xml, which is easy to forget.

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

Next, we will test the layout file code on a simple interface.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Enter the number to call:"/> <EditText android: id = "@ + id/etPhone" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: inputType = "phone"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onClickActionCall" android: text = "ACTION_CALL"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onClickActionDial" android: text = "ACTION_DIAL open dialing interface"/> </LinearLayout>

The corresponding Activity code is as follows:

Package chengyujia. androidtest; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. view; import android. widget. editText; import android. widget. toast; public class CallActivity extends Activity {private EditText etPhone; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContent View (R. layout. activity_call); etPhone = (EditText) findViewById (R. id. etPhone);} // call by ACTION_CALL (direct call) public void onClickActionCall (View v) {// Intent. ACTION_CALL is actually a specific string, // ACTION_CALL = "android. intent. action. CALL ", // tell the system that I want to dial. Call (Intent. ACTION_CALL);} // call by ACTION_DIAL (open the dialing Interface) public void onClickActionDial (View v) {// Similarly, Intent. ACTION_DIAL is also a specific string // ACTION_DIAL = "android. intent. action. DIAL "// tell the system that I want to open the dialing interface and display the dialing number on the dialing interface. It is up to the user to decide whether to DIAL. Call (Intent. ACTION_DIAL);} private void call (String action) {String phone = etPhone. getText (). toString (); if (phone! = Null & phone. trim (). length ()> 0) {// here, "tel:" + the phone number is in a fixed format. The system will see it starting with "tel:" and it will be followed by a phone number. Intent intent = new Intent (action, Uri. parse ("tel:" + phone. trim (); startActivity (intent); // call the intent above to implement dialing} else {Toast. makeText (this, "the phone number cannot be blank", Toast. LENGTH_LONG ). show ();}}}

Run the following command to check the effect.

The interface is as follows:

I have entered the phone number 10086. Next, click the first button "call by ACTION_CALL ",

As follows:

I found that I didn't dial it out directly. Instead, I gave the user a prompt asking the user to choose whether to dial the number. This also prevents others from doing evil. Science and technology should make life better, not worse, but not everyone thinks so, so they have to prevent it. The system is right. Let's continue the test. Click "Allow once" to start dialing, as shown below:

Go back to the test page and click "ACTION_DIAL open dialing interface:

 

This is the dial-up interface of the system. At the same time, the number to be dialed has been written to the user. It is up to the user to decide whether to dial the number.

Which method is used in actual development depends on the actual situation. Well, let's write about the dialing function of Android apps.

Work is not the whole of life. Let's end up with a funny joke.

Old Bodhi called Wukong to the front of his body: "You have learned the immortality and the seventy-two changes. Today, I want to teach you new spells ." Wukong said, "What is the spell ?" Bodhi's old ancestor said, "Have you seen the clouds on this day? There are seven clouds here, and there are five clouds there. How many clouds are there ?" Wukong answered: "Twelve ." "Well, what I want to teach you is cloud computing ."

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.