編輯:關於 Android 程式設計
Intent 是 Android 元件之間溝通的橋樑,而 Action 則是它宣告「我想做什麼」的字串常數。把常用的 Action 整理成一份對照表,寫程式時會省下不少查文件的時間。
String: android.intent.action.MAIN
標識 Activity 為一個程式的進入點,最常與 CATEGORY_LAUNCHER 搭配出現在 AndroidManifest 中。
Input: 無 Output: 無
String: android.intent.action.CALL
直接撥打指定號碼,注意這會跳過撥號畫面直接通話,需要 CALL_PHONE 權限。
Input: 電話號碼,格式為 tel:+phone number Output: 無
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:13200100001"));
startActivity(intent);
String: android.intent.action.DIAL
開啟撥號面板並帶入號碼,但不會直接撥出,使用者仍需按撥號鍵。相較於 ACTION_CALL,這個不需要任何權限,實務上更常用。
Input: 電話號碼,格式為 tel:+phone number Output: 無
說明:若未設定 data,會開啟一個空白的撥號 UI;若設定了 data,則透過 getData() 取得號碼並填入。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL); // android.intent.action.DIAL
intent.setData(Uri.parse("tel:13200100001"));
startActivity(intent);
String: android.intent.action.ALL_APPS
列出所有應用程式。Input: 無 Output: 無
String: android.intent.action.ANSWER
處理來電。Input: 無 Output: 無
String: android.intent.action.ATTACH_DATA
指定某些資料應該附加到其他地方,例如把一張圖片附加到聯絡人。Input: Data Output: 無
String: android.intent.action.BUG_REPORT
顯示 Bug 回報畫面。Input: 無 Output: 無
String: android.intent.action.CALL_BUTTON
等同使用者按下「撥號」鍵,實測顯示的是通話記錄畫面。Input: 無 Output: 無
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON); startActivity(intent);
String: android.intent.action.CHOOSER
顯示一個 Activity 選擇器,讓使用者在眾多候選中挑一個,通常搭配 ACTION_GET_CONTENT 使用。
String: android.intent.action.GET_CONTENT
讓使用者選擇特定型別的資料並回傳,例如拍一張相片或錄一段音。Input: Type Output: URI
int requestCode = 1001;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*"); // 若要選影片則改為 video/*,不限型別則用 */*
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);
String: android.intent.action.VIEW
用來顯示使用者的資料。這是相當通用的 Action,系統會依 URI 的 scheme 自動挑選合適的 Activity: https:// 開瀏覽器,tel: 開撥號程式,geo: 開地圖定位。
Uri uri = Uri.parse("https://www.google.com"); // 瀏覽器
Uri uri = Uri.parse("tel:1232333"); // 撥號程式
Uri uri = Uri.parse("geo:39.899533,116.036476"); // 地圖定位
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
// 播放影片
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/media.mp4");
intent.setDataAndType(uri, "video/*");
startActivity(intent);
// 呼叫傳送簡訊的程式
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "訊息內容...");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
String: android.intent.action.SENDTO
傳送簡訊。
// 傳送簡訊
Uri uri = Uri.parse("smsto:13200100001");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "訊息內容...");
startActivity(it);
// 傳送多媒體訊息,系統會提示選擇合適的程式
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "內容");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
// 寄送 Email
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = {"android1@163.com"};
String[] ccs = {"you@yahoo.com"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
同一個 Action 搭配不同 type,就能開出各種媒體選擇器:
// 選擇圖片,requestCode 為回傳標識
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
// 選擇音訊
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
// 錄製影片
int durationLimit = getVideoCaptureDurationLimit();
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
// 選擇影片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
// 錄音
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/amr");
intent.setClassName("com.android.soundrecorder",
"com.android.soundrecorder.SoundRecorder");
((Activity) context).startActivityForResult(intent, requestCode);
// 拍照,REQUEST_CODE_TAKE_PICTURE 為回傳標識
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
Action 字串只是約定,能否被處理取決於裝置上是否裝了對應的程式。上表的 com.android.soundrecorder 這類寫死 class name 的做法,在許多客製化 ROM 上並不存在,實務上應先透過 PackageManager.queryIntentActivities() 確認有沒有可用的目標,再決定要呼叫或是給使用者提示。
另外,從 Android 11(API 30)開始引入了套件可見性限制,若要在程式碼中查詢其他 App 的元件,需要在 manifest 中以 <queries> 明確宣告,否則查詢結果會是空的。
Android 簽名、證書與公私鑰完整解析
公私鑰的基本概念現代密碼學中的公開金鑰系統,又稱為非對稱式加密或雙金鑰系統。每個使用者都會擁有一組金鑰:公鑰與私鑰。公鑰可以公開給他人使用,私鑰則必須由本人妥善保管。兩把
Android 手機衛士實戰:綁定 SIM 卡序列號與防盜攔截
接著處理導航流程中的第二個引導頁——SIM 卡綁定。 這個功能的思路是:把當前 SIM 卡的序列號存進 SharedPreferences,日後一旦換卡,就能對比出異常
Android 打造類似 Google Play 風格的 Tab 選單
近來需要實作類似 Google Play 的滑動式 Tab 效果,本文整理相關的參考資源與實作歷程,示範如何結合 Navigation Drawer、ViewPager
Android 網絡編程(二):HttpClient 與 HttpURLConnection 實戰
前言 上一篇我們了解了 HTTP 協議原理,這一篇來談談 Apache 的 HttpClient 與 Java 的 HttpURLConnection。這兩者都是我們平常