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

Android 學習筆記:模擬撥打電話的實作教學

編輯:關於 Android 程式設計

撥打電話在 Android 中是透過 Intent 系統完成的,本質上是把號碼包裝成 URI,交給系統選擇合適的應用來處理。這個範例示範一個輸入號碼後按下按鈕即可撥號的小程式。

一、字串資源 string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, SharpPhoneActivity!</string>
    <string name="app_name">SharpPhone</string>
    <string name="mobile">請輸入手機號</string>
    <string name="button">撥打此號</string>
</resources>

二、版面配置 main.xml

一個提示文字、一個號碼輸入框、一顆按鈕,結構非常單純。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/mobile" />

    <EditText
        android:id="@+id/phoneno"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />
</LinearLayout>

三、Activity 程式碼

按下按鈕時取出輸入框的號碼,做一次簡單的內容檢查,然後建立撥號 Intent 並啟動。

package com.sharpanroid.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 SharpPhoneActivity extends Activity {

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

        Button bt = (Button) this.findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText phoneText = (EditText) findViewById(R.id.phoneno);
                String phoneno = phoneText.getText().toString();

                if (null != phoneno && !"".equals(phoneno.trim())) {
                    // 以 tel: 協定包裝號碼,建立撥號意圖
                    Intent intent = new Intent(Intent.ACTION_CALL,
                            Uri.parse("tel:" + phoneno));
                    startActivity(intent);
                }
            }
        });
    }
}

程式碼中那個空值與空字串檢查看似多餘,實則必要。若直接送出空號碼,系統會開啟撥號程式卻沒有號碼,或者直接拋出例外,都是不好的體驗。

四、清單檔案中宣告權限

因為呼叫了撥號服務,必須在 AndroidManifest.xml 中申請 CALL_PHONE 權限,否則會得到 SecurityException。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.sharpanroid.phone"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SharpPhoneActivity"
            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>
</manifest>

五、兩種 Action 的差別與實務建議

Android 中與電話相關的 Intent 主要有兩種,選錯會直接影響使用者體驗,也影響上架審核:

  • ACTION_CALL:按下按鈕立刻撥出,使用者沒有確認的機會。需要 CALL_PHONE 這個高風險權限,Google Play 對此類權限的審查相當嚴格,一般應用不建議使用。
  • ACTION_DIAL:只把號碼帶到撥號介面,由使用者親手按下通話鍵。不需要任何權限,也符合多數應用商店的規範。

若只是要提供「聯絡我們」這類功能,強烈建議改用 Intent.ACTION_DIAL,把 Uri.parse("tel:" + phoneno) 傳入即可,程式碼幾乎一樣,但省下權限申請與審核的麻煩。

另外,較新版本的 Android 在執行期需要動態請求 CALL_PHONE 權限,僅在清單中宣告是不夠的。使用模擬器測試時,若沒有預先設定模擬器支援通話,撥號動作可能只會停在撥號介面而不會真的接通。

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