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

Android 撥號器功能的簡單實現

編輯:關於 Android 程式設計

需求很簡單:界面上放一個輸入框和一個「撥打電話」按鈕,用戶輸入號碼後點擊按鈕,跳轉到系統撥號介面並帶入號碼。這是一個非常典型的「Intent 隱式調用系統組件」的練習。

一、佈局實現

使用垂直方向的 LinearLayout,內部依次放置提示文字、輸入框和按鈕:

<LinearLayout 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:orientation="vertical"
    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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請輸入電話" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />

    <Button
        android:id="@+id/callsumbit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="撥打電話" />

</LinearLayout>

這裡有一個值得注意的屬性:android:inputType="phone"。它會讓系統自動彈出數字鍵盤,並允許輸入 *、#、+ 等撥號相關字符,比使用默認的文本輸入體驗好得多。

二、Activity 代碼

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call);

    Button btn = (Button) findViewById(R.id.callsumbit);
    btn.setOnClickListener(new OnClickListener() {

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

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + number));
            startActivity(intent);
        }
    });
}

核心就是三行:ACTION_CALL 指定動作,tel: 開頭的 Uri 指定數據,然後 startActivity。系統會根據 Intent 的 action 與 data 自動匹配能處理它的 Activity,也就是系統電話應用。

三、權限聲明

直接撥打電話屬於敏感操作,必須在 AndroidManifest.xml 中聲明權限:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.ccec.callphone.CallActivity"
            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 的區別(重點)

實際開發中,這裡有一個必須權衡的選擇:

Action行為是否需要權限
Intent.ACTION_CALL直接撥號,立即打出需要 CALL_PHONE(危險權限,需運行時申請)
Intent.ACTION_DIAL打開撥號盤並自動填入號碼,由用戶確認後再撥無需任何權限

兩者的實現代碼幾乎一樣,只不過把 action 換成 Intent.ACTION_DIAL 即可:

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);

從用戶體驗與上架審核的角度,大多數業務場景更推薦使用 ACTION_DIAL。原因有三:其一,避免用戶誤觸直接撥出高額付費電話;其二,讓用戶在撥出前有一次確認機會,符合通話類應用的普遍交互慣例;其三,省掉了危險權限的運行時申請流程,代碼更簡潔,也不會因為用戶拒絕授權而導致功能不可用。

如果確實需要一鍵直撥(例如客服熱線、緊急聯絡場景),那麼在 Android 6.0(API 23)及以上版本還必須實現運行時動態申請:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);
} else {
    // 已授權,直接撥打
}

五、其他實用細節

  • 撥打前校驗號碼非空,避免傳入空字串導致 ActivityNotFoundException。
  • 用 Uri.encode(number) 對號碼做編碼,防止特殊字符引發解析異常。
  • 若設備無電話功能(部分平板),可用 resolveActivity() 先判斷是否有可處理的應用,再決定是否提示。

小結

這個例子雖然簡單,但涵蓋了 Android 開發中幾個核心知識點:佈局中控件的 inputType 屬性、Intent 的 action 與 data 組合、權限聲明,以及 ACTION_CALL 與 ACTION_DIAL 的場景取捨。理解了「Intent 通過 action + data 匹配組件」這一機制,後續調用系統相機、打開網頁、發送郵件、分享文本,套路完全一致。

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