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

Android 撥打電話實作:Intent、Uri 與權限三件事

編輯:Android 開發實例

打電話和傳簡訊是手機最基礎的能力,因此在入門課程裡,「用程式撥號」幾乎是必做的練習。它牽涉到的觀念很典型:組出一個帶 data 的 Intent、宣告權限、再處理輸入為空的狀況。以下從建立專案開始走一遍。

步驟一:建立專案

新增一個 Android 專案,命名為 phoneCallDemo,套件結構維持預設即可。

步驟二:設計輸入介面

畫面只需要三個元件:一段提示文字、一個電話輸入框、一顆撥號鈕。輸入框加上 android:phoneNumber="true",鍵盤就會自動切換成數字與號碼相關符號。

<?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:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="撥打電話" />
</LinearLayout>

步驟三:加上撥號權限

直接撥號屬於敏感操作,必須在 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">

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

    <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>
</manifest>

補充一個現代開發必須知道的背景:CALL_PHONE 屬於危險權限等級,在 Android 6.0 以後除了宣告,還要在執行期透過 requestPermissions() 向使用者取得同意,否則會直接收到 SecurityException。

步驟四:主程式邏輯

public class PhoneCallDemo extends Activity {

    private Button bt;
    private EditText et;

    @Override
    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 View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputStr = et.getText().toString().trim();

                if (inputStr.length() == 0) {
                    Toast.makeText(PhoneCallDemo.this,
                            "號碼不能為空", Toast.LENGTH_LONG).show();
                    return;
                }

                Intent phoneIntent = new Intent(Intent.ACTION_CALL,
                        Uri.parse("tel:" + inputStr));
                startActivity(phoneIntent);
            }
        });
    }
}

程式的骨架其實只有一行重點:Uri.parse("tel:" + 號碼)。這個 tel: 是系統約定好的 URI scheme,搭配 ACTION_CALL 會直接撥出;若改用 ACTION_DIAL,系統只會把號碼帶進撥號盤、由使用者按下通話鍵才撥出。

兩種 action 的取捨

  • ACTION_CALL:一鍵撥出,需要 CALL_PHONE 權限,適合客服專線、緊急聯絡等明確情境。
  • ACTION_DIAL:只帶號碼開啟撥號盤,不需敏感權限,也讓使用者有機會確認號碼是否正確,多數 App 用這個即可。

無論選哪一種,都建議在組 Intent 前包上 try-catch 處理 ActivityNotFoundException,並對輸入內容做一次格式檢查——畢竟號碼會被送進系統的通話流程,錯誤字元沒有理由傳下去。

這個小練習示範的正是 Android 元件互動的標準套路:介面用 XML 描述、事件用 Listener 串接、跨應用能力用 Intent 表達、敏感能力用權限把關。把 tel: 換成 mailto:、geo: 或 https:,同一份程式碼就變成另一個功能了。

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