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

Android Intent 各種用法總整理:網頁、地圖、電話、簡訊、郵件一把抓

編輯:關於 Android 程式設計

Intent 可以說是 Android 平台最具代表性的設計之一。它的核心概念很簡單:你只要宣告「我想做什麼」(Action)以及「要拿什麼資料去做」(Data),剩下的交給系統。呼叫 startActivity() 之後,Android 會自動找出所有能勝任這件事的 App,挑出最合適的那一個來執行;如果有多個候選,甚至會讓使用者自己選。

這正是 Intent 的價值所在——你的 App 不需要自己實作瀏覽器、地圖或撥號器,只要把意圖表達清楚,就能借用系統裡既有的能力。以下依情境整理常見用法。

顯示網頁

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

顯示地圖座標

用 geo: 這個 URI scheme,後面接緯度、經度(注意順序是「緯度,經度」):

Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

路徑規劃

把起點與終點座標拼進 Google Maps 的查詢字串,即可直接叫出路線規劃畫面:

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:0800000123");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);

若想直接撥出,改用 ACTION_CALL,並記得在 AndroidManifest.xml 中申請權限:

Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
<uses-permission android:name="android.permission.CALL_PHONE" />

叫出簡訊程式並帶入內容

Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);

直接指定收件號碼傳送簡訊

smsto: 之後接上對方號碼,用 ACTION_SENDTO:

Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);

發送多媒體訊息(彩信)

關鍵在於 EXTRA_STREAM 帶入附件 URI,並用 setType() 宣告 MIME 類型:

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);

需要主旨與內文時,改用 ACTION_SEND,並用 createChooser 讓使用者挑郵件客戶端:

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"));

要同時指定收件者、副本、主旨,把對應的 extra 換成陣列形式即可:

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"));

播放多媒體檔案

用 setDataAndType() 一次把 URI 與 MIME 類型都設定好:

Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);

也可以走 MediaStore,播放媒體資料庫中的指定項目:

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);

小結

把這些範例放在一起看,可以歸納出幾個固定套路:'action 決定要做什麼,Uri 決定對誰做,putExtra 決定帶什麼額外資訊,setType 決定資料是什麼格式。掌握這四個要素,面對任何 Intent 需求都能快速拼出正確寫法。

另外提醒兩點:一是 CALL_PHONE、讀寫外部儲存空間這類敏感權限需要在 Manifest 中明確宣告,較新的 Android 版本還要加上執行期動態請求;二是從 Android 7.0 開始,直接傳 file:// URI 給其他 App 會引發 FileUriExposedException,此時應改用 FileProvider 產生 content:// 形式的 URI,並記得用 FLAG_GRANT_READ_URI_PERMISSION 授權。

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