Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android 基礎教學:五步驟實作簡易撥打電話程式

編輯:開發入門

對一支手機來說,能撥打電話是最重要也最常用的功能。那麼在 Android 中該如何實作撥號程式呢?以下用一個簡單的 Demo,分五個步驟帶你完成。

Step 1:建立工程

新增一個 Android 專案,命名為 phoneCallDemo。

Step 2:設計介面

打開 main.xml,放入一個提示文字、一個輸入號碼的 EditText 與一個撥號按鈕:

<?XML version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please input the phoneNumber:"
    />
<EditText
    android:id="@+id/et1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:phoneNumber="true"
    />
<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Call Phone"
    />
</LinearLayout>

這裡的 android:phoneNumber="true" 會讓輸入框自動切換成電話鍵盤並限制可輸入字元,比自己做輸入驗證省事。

Step 3:加上撥號權限

要直接撥號,必須在 AndroidManifest.xml 中宣告 CALL_PHONE 權限,否則程式會在執行時被系統拒絕:

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.android.test"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PhoneCallDemo"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <!-- 添加撥出電話的權限 -->
    <uses-permission android:name="android.permission.CALL_PHONE">
    </uses-permission>
</manifest>

Step 4:主程式

package com.android.test;

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;
import android.widget.Toast;

public class PhoneCallDemo extends Activity {

    private Button bt;
    private EditText et;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 取得控制項
        bt = (Button)findViewById(R.id.bt1);
        et = (EditText)findViewById(R.id.et1);

        // 掛上事件回應
        bt.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {

                // 取得輸入的電話號碼字串
                String inputStr = et.getText().toString();

                // 如果輸入不為空,建立撥號的 Intent
                if(inputStr.trim().length()!=0)
                {
                    Intent phoneIntent = new Intent("android.intent.action.CALL",
                            Uri.parse("tel:" + inputStr));
                    // 啟動
                    startActivity(phoneIntent);
                }
                // 否則用 Toast 提示一下
                else{
                    Toast.makeText(PhoneCallDemo.this, "不能輸入為空",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

程式的關鍵是 Intent 的 action 與 data:action 使用 android.intent.action.CALL 表示直接撥出,data 則是以 tel: 開頭的 URI。系統收到這個意圖後,會自動找到對應的撥號程式完成撥號,這正是 Android 鬆耦合架構的體現。

實務上要注意:ACTION_CALL 會直接撥出,屬於高風險行為,因此除了宣告權限,較新的 Android 版本還需要執行期動態請求權限。若你只想把號碼帶到撥號面板由使用者確認,可以改用 Intent.ACTION_DIAL,那樣就不需要 CALL_PHONE 權限。

Step 5:執行結果

程式主畫面

輸入號碼後點擊撥號

輸入為空時的 Toast 提示

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