Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 兩種程式化撥號方式:ACTION_CALL 與 ACTION_DIAL

編輯:關於 Android 開發

想在 App 裡加一個撥號功能,Android 提供了兩條路: ACTION_CALL 直接撥出,ACTION_DIAL 則是開啟系統撥號盤並帶入號碼,由使用者決定是否按下通話鍵。

先加權限

要在 AndroidManifest.xml 中宣告撥號權限,這一步很容易漏掉:

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

版面配置

一個輸入框加上兩個按鈕,分別對應兩種方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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="請輸入要撥打的號碼:" />

    <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 方式打開撥號界面" />

</LinearLayout>

Activity 程式碼

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);
        setContentView(R.layout.activity_call);
        etPhone = (EditText) findViewById(R.id.etPhone);
    }

    // ACTION_CALL:直接撥打
    public void onClickActionCall(View v) {
        // ACTION_CALL 實際就是字串 "android.intent.action.CALL"
        call(Intent.ACTION_CALL);
    }

    // ACTION_DIAL:開啟撥號界面
    public void onClickActionDial(View v) {
        // ACTION_DIAL 實際就是字串 "android.intent.action.DIAL"
        call(Intent.ACTION_DIAL);
    }

    private void call(String action) {
        String phone = etPhone.getText().toString();
        if (phone != null && phone.trim().length() > 0) {
            // "tel:" + 號碼 是固定格式,系統看到 tel: 開頭就知道後面是電話號碼
            Intent intent = new Intent(action, Uri.parse("tel:" + phone.trim()));
            startActivity(intent);
        } else {
            Toast.makeText(this, "電話號碼不能為空", Toast.LENGTH_LONG).show();
        }
    }
}

實測結果

輸入 10086 後點擊第一顆按鈕:

並沒有直接撥出,而是跳出確認提示:

這是系統刻意的安全設計——防止程式在背景任意撥打付費電話。按下「允許一次」後才會真正撥號:

再回來測第二顆按鈕,ACTION_DIAL 直接開啟系統撥號盤:

號碼已經替你填好,要不要撥由使用者決定。

該選哪一種

若追求操作步驟最少、路徑最短,用 ACTION_CALL;若希望把決定權交給使用者、避免誤觸爭議,用 ACTION_DIAL。另外提醒:Android 6.0 以上採取執行期權限機制,使用 ACTION_CALL 前必須先動態向使用者請求 CALL_PHONE 權限,否則會被系統攔下。

最後放個段子換換心情:

菩提老祖將悟空喚至身前:「你已學會長生不老術和七十二變,今日為師欲傳授你新的法術。」悟空道:「是何法術?」菩提老祖道:「看到這天上的雲彩了嗎?這邊有七朵雲彩,那邊有五朵雲彩,一共有幾朵?」悟空答:「十二朵。」菩提老祖道:「嗯,我要教你的就是雲計算。」

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved