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

Android 跳轉至系統聯絡人、撥號與簡訊介面的實作整理

編輯:關於 Android 程式設計

在 Android 開發中,常需要從自家應用程式跳到系統內建的撥號、聯絡人、簡訊等介面。Android 透過 Intent 提供這些能力,但不同 Action 的行為與權限要求並不相同。以下整理常見用法,方便依需求選擇。

一、跳轉撥號或通話介面

若只是把號碼帶到撥號鍵盤,建議使用 ACTION_DIAL。這種方式通常不需要直接撥號權限,也能降低誤觸風險。

Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
startActivity(dialIntent);

若應用程式本身已取得通話權限,且要直接發起通話,可使用 ACTION_CALL。在 Android 6.0 以上,仍需於執行期動態請求 CALL_PHONE 權限。

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);

二、開啟聯絡人與新增聯絡人

開啟系統聯絡人清單,可使用 ACTION_PICK 搭配 Contacts Provider 的 URI。

Intent pickIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivity(pickIntent);

若要新增或編輯聯絡人,可使用 ACTION_INSERT_OR_EDIT,並透過 putExtra 帶入姓名、電話等資訊。

Intent insertIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
insertIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
insertIntent.putExtra(ContactsContract.Intents.Insert.NAME, "王小明");
insertIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "0912345678");
startActivity(insertIntent);

三、開啟簡訊介面

要開啟簡訊撰寫畫面並帶入收件者與內容,可使用 smsto URI,搭配 ACTION_SENDTO 或 ACTION_VIEW。

Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:0912345678"));
smsIntent.putExtra("sms_body", "這是一段測試簡訊");
startActivity(smsIntent);

部分機型或系統版本對 setClassName 指定內建應用程式類別的相容性不佳,正式專案建議優先使用標準 Action,避免硬編碼套件名稱與 Activity 名稱。

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