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

Android 簡易電話撥號器

編輯:Android 開發實例

實作概念

Android 可透過 Intent 快速撥打電話。若只是要讓使用者輸入號碼後按下按鈕撥出,可使用 ACTION_CALL;若希望先進入撥號畫面由使用者確認,則可使用 ACTION_DIAL。

版面配置

畫面可包含一個輸入框與一個按鈕:

EditText phone = findViewById(R.id.phone);
Button button = findViewById(R.id.button);

建立撥號 Intent

String tel = phone.getText().toString();
Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + tel));
startActivity(intent);

若要使用更明確的常數,也可寫成:

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

權限設定

直接撥號需要通話權限:

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

在 Android 6.0 以後,此權限屬於危險權限,需要在執行期向使用者請求。

注意事項

若使用 ACTION_DIAL,通常不需要 CALL_PHONE 權限,但使用者仍需自行按下撥號鍵。直接撥號較方便,但也更容易造成誤觸或濫用,建議加入二次確認或僅在受控情境使用。

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