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

Android 實作電話撥號器:Intent 與 CALL_PHONE 權限

編輯:Android 開發實例

做一個能輸入號碼並直接撥出的簡單 App,是理解 Intent 跨元件溝通的好練習。以下拆解三個檔案。

一、AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="eoe.phone"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity android:name=".MainActivity"
            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="7" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

<uses-permission android:name="android.permission.CALL_PHONE"/> 這行不能漏。少了它,程式在執行撥號時會直接拋出 SecurityException 而崩潰。這個權限屬於危險等級,Android 6.0 以後還需要在執行期動態向使用者申請。

二、main.xml 版面配置

<?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="請輸入電話號碼" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拔打此號碼"
        android:id="@+id/button" />
</LinearLayout>

三、MainActivity 程式碼

package com.ljq.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 MainActivity extends Activity {
    private EditText phone = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        phone = (EditText) this.findViewById(R.id.phone);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String tel = phone.getText().toString();

                // 方法一:使用 Intent,目的為激活 android 元件
                // Intent intent = new Intent();
                // intent.setAction("android.intent.action.CALL");
                // intent.setData(Uri.parse("tel:" + tel));

                // 方法二
                Intent intent = new Intent("android.intent.action.CALL",
                        Uri.parse("tel:" + tel));
                // 該方法內部會自動為 intent 對象設置類別:
                // android.intent.category.DEFAULT
                startActivity(intent);
            }
        });
    }
}

兩種寫法與一個重要選擇

程式碼中示範了兩種等價的 Intent 建構方式:先用空建構子再分別設定 action 與 data,或是直接用雙參數建構子一次完成。兩者結果完全相同,後者較簡潔。

但真正該思考的是用 CALL 還是 ACTION_DIAL:

  • ACTION_CALL(本例採用):按下按鈕就直接撥出,需要 CALL_PHONE 權限,且必須通過執行期權限申請。使用者沒有最後確認的機會。
  • ACTION_DIAL:只把號碼帶到系統撥號盤並自動填入,使用者還要再按一次撥號鍵。優點是不需要任何權限,也不會有誤撥風險。

除非你的 App 本身就是電話應用(例如企業內部的客服系統),否則建議使用 ACTION_DIAL。Google Play 對使用 CALL_PHONE 權限的審核相當嚴格,若無法清楚說明必要性,上架會被退回。

另外補充兩個實務細節:輸入框可加上 android:inputType="phone" 讓鍵盤直接跳數字鍵盤;送出前應檢查號碼是否為空或格式不合法,避免帶著空字串去啟動撥號器。

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