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

Android 撥打電話實作:用 ACTION_CALL 一鍵直撥

編輯:關於 Android 程式設計

「撥打電話」看似簡單,卻牽涉到意圖(Intent)、URI 以及權限三個環節。本文以 lession01_phone 專案為例,說明如何讓使用者輸入號碼後,按下按鈕即可直接撥出。

建立專案與版面構思

先建立撥打電話專案,版面概念很單純:一個提示文字、一個號碼輸入框、一顆撥打按鈕。

一、布局檔 activity_phone.xml

使用 RelativeLayout,透過 layout_below 讓輸入框與按鈕依序排在提示文字下方,並用 inputType="phone" 叫出電話鍵盤:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://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=".PhoneActivity" >

    <TextView
        android:id="@+id/viewphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone" />

    <EditText
        android:id="@+id/editphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/viewphone"
        android:layout_below="@+id/viewphone"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="phone" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editphone"
        android:layout_below="@+id/editphone"
        android:layout_marginTop="23dp"
        android:text="@string/buttonphone" />

</RelativeLayout>

二、字串資源 strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">lession01_phone</string>
    <string name="action_settings">Settings</string>
    <string name="phone">撥打電話</string>
    <string name="buttonphone">撥打</string>

</resources>

同樣地,記得在 AndroidManifest.xml 中加入 CALL_PHONE 權限,否則 ACTION_CALL 無法直接撥號。

三、Java 邏輯 PhoneActivity.java

關鍵在於建立一個帶有 tel: 前綴 URI 的 ACTION_CALL 意圖,把它交給 startActivity 後,系統就會進入撥號流程:

package com.example.lession01_phone;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PhoneActivity extends Activity {

    private Button button_phone;
    private EditText edit_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);

        button_phone = (Button) findViewById(R.id.btnphone);
        edit_phone = (EditText) findViewById(R.id.editphone);

        button_phone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String phone_Num = edit_phone.getText().toString();

                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone_Num));

                PhoneActivity.this.startActivity(intent);

                Toast.makeText(PhoneActivity.this, "正在給" + phone_Num + "撥打", Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.phone, menu);
        return true;
    }
}

補充一個實務上的選擇:若改用 Intent.ACTION_DIAL,系統只會跳轉到撥號盤並帶入號碼,由使用者自行按下撥打,這種方式不需要 CALL_PHONE 權限,對使用者也較友善;而 ACTION_CALL 則是直接撥出,屬於較敏感的操作。

執行成果如下:

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