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

Android Intent 常用寫法速查:網頁、地圖、電話、簡訊、Email 與解除安裝

編輯:關於 Android 程式設計

Intent 是 Android 特有的機制。你在 Intent 裡描述「要做什麼動作」(檢視、編輯、撥號…)以及「這個動作需要的資料」,交給 startActivity(),系統就會自動找出最符合條件的 App 並啟動它。以下按用途整理常見的寫法。

瀏覽網頁

Uri uri = Uri.parse("https://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

顯示地圖與規劃路線

// 以經緯度定位
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
// 路徑規劃:saddr 為起點,daddr 為終點
Uri uri = Uri.parse("https://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

撥打電話

// 只開啟撥號面板,由使用者按下通話鍵
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
// 直接撥出,必須在 Manifest 宣告 CALL_PHONE 權限
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);

<uses-permission id="android.permission.CALL_PHONE" /> 屬於高風險權限,上架審核會要求說明;能改用 ACTION_DIAL 就別用 ACTION_CALL。

傳送簡訊與多媒體訊息

// 叫出簡訊 App 並帶入內容
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
// 直接指定收件人
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
// 附加圖片(彩信)
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);

寄送 Email

// 指定收件人
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
// 由使用者挑選郵件客戶端
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
// 完整欄位:收件人、副本、主旨、內文
Intent it = new Intent(Intent.ACTION_SEND);
String[] tos = { "me@abc.com" };
String[] ccs = { "you@abc.com" };
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
// 加上附件
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
it.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));

播放多媒體

// 播放 SD 卡上的 mp3
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
// 播放 MediaStore 中的第 1 首
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

解除安裝

Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);

幾個實務提醒

  • 先確認有人接得住: implicit Intent 若沒有任何 App 符合,startActivity() 會拋出 ActivityNotFoundException。用 sendIntent.resolveActivity(getPackageManager()) != null 先檢查,或用 try/catch 包住。
  • 附件與檔案 URI:Android 7.0 以後 file:// URI 會觸發 FileUriExposedException,必須改用 FileProvider 產生 content:// URI 並以 FLAG_GRANT_READ_URI_PERMISSION 授權。
  • 用 createChooser:涉及多個候選 App 時,Intent.createChooser() 的體驗比讓系統自行挑選更好,也避免使用者困惑。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved