編輯:關於 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);
最簡單的寫法,直接指定收件者:
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 授權。
Android 仿京東畫軸效果:ScrollPaintView 自定義控件實現
京東早期首頁有個很有意思的交互:一個可以上下拖動的「畫軸」,往下拉就把一幅畫徐徐展開。當時我的第一想法是動態改變 ImageView 的高度,效果基本能出來,但滑動部分的
Buzz 桌面教學:把應用程式圖示加到桌面
Buzz 桌面裝好之後,並不會自動把手機裡已安裝的程式圖示排到桌面上,得自己手動加上去。以下說明兩種做法。 Buzz 桌面新增圖示方法 1.在待機桌面用兩指往下滑,或連續
Android 自定義 View 完全指南:擴充、複合與全新元件
自定義 View 的起點 在 Android 中,自定義 View 是打造專屬 UI 的重要方式。通常我們會覆寫 onDraw 來繪製畫面,如果需要支援 wrap_con
用 Android 登入案例徹底搞懂狀態模式(二)
上一篇用簡單的 Java 範例熟悉了狀態設計模式,這一篇則以 Android 登入的例子繼續講解,讓抽象的設計模式落到實處。 一、狀態模式在 Android 中的應用場景