編輯:關於 Android 程式設計
Intent 是 Android 組件之間溝通的核心。這一篇整理如何從自己的應用啟動別人的 Activity、如何自訂 action 讓外部來呼叫,以及常用系統 action 與設定頁面的對照表。
寫法一:指定 ComponentName。
Intent i = new Intent();
ComponentName cn = new ComponentName("com.book.android2", "com.book.android2.AndroidSearch");
i.setComponent(cn);
i.setAction("android.intent.action.MAIN");
startActivity(i);
這個寫法涵蓋率不錯,但實測部分應用(例如 RootExplorer)會直接閃退跳出 FC 對話框。遇到這種情況,改用下面的寫法。
寫法二:setClassName。
Intent it = new Intent("android.intent.action.MAIN");
it.setClassName("com.speedsoftware.rootexplorer",
"com.speedsoftware.rootexplorer.RootExplorer");
startActivity(it);
用 adb 啟動。不寫程式也能驗證是否啟動成功:
am start -a android.intent.action.MAIN -n com.speedsoftware.rootexplorer/.RootExplorer am start -a android.settings.SETTINGS
先在被呼叫的那個應用的 AndroidManifest.xml 中宣告自訂 action:
<intent-filter>
<action android:name="com.qylk.call.main" /> <!-- 自訂 action -->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
其他位置要啟動它就變得非常直觀:
Intent it = new Intent("com.qylk.call.main");
startActivity(it);
// Google 搜尋
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "searchString");
startActivity(intent);
// 開啟網頁
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com")));
// 顯示座標
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:38.899533,-77.036476")));
// 路線規劃
Uri uri = Uri.parse("https://maps.google.com/maps?f=d&saddr=startLat startLng"
+ "&daddr=endLat endLng&hl=en");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
Uri uri = Uri.parse("tel:xxxxxx");
startActivity(new Intent(Intent.ACTION_DIAL, uri));
// 直接撥號需加上 uses-permission 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);
// 直接帶號碼與內容
Uri uri = Uri.parse("smsto:0800000123");
Intent it2 = new Intent(Intent.ACTION_SENDTO, uri);
it2.putExtra("sms_body", "The SMS text");
startActivity(it2);
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);
// 最簡單:只給收件人
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:xxx@abc.com")));
// 帶主旨與本文
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{"me@abc.com"});
it.putExtra(Intent.EXTRA_CC, new String[]{"you@abc.com"});
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
// 帶附件
Intent att = new Intent(Intent.ACTION_SEND);
att.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
att.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
att.setType("audio/mp3");
startActivity(Intent.createChooser(att, "Choose Email Client"));
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
// 播放媒體庫中的指定曲目
Uri mediaUri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
startActivity(new Intent(Intent.ACTION_VIEW, mediaUri));
// 移除
Uri uri = Uri.fromParts("package", strPackageName, null);
startActivity(new Intent(Intent.ACTION_DELETE, uri));
// 安裝(亦可監聽 ACTION_PACKAGE_ADDED 廣播)
Uri installUri = Uri.fromParts("package", "xxx", null);
Intent returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
// 觸發拍照
ContentValues values = new ContentValues(7);
values.put(Images.Media.TITLE, fileName);
values.put("_data", fileName);
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName);
Uri photoUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photo.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(photo, 10);
// 從相簿挑圖
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, 11);
startActivity(new Intent(Media.RECORD_SOUND_ACTION));
// 應用詳情頁(用 package name 最省事)
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=")));
// 搜尋應用
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:pkg_name")));
// 挑選號碼
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("vnd.android.cursor.item/phone");
startActivityForResult(i, REQUEST_TEXT);
// 新增或編輯聯絡人
Intent it = new Intent(Intent.ACTION_INSERT_OR_EDIT);
it.setType(People.CONTENT_ITEM_TYPE);
it.putExtra(Contacts.Intents.Insert.NAME, "My Name");
it.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
it.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);
it.putExtra(Contacts.Intents.Insert.EMAIL, "com@com.com");
it.putExtra(Contacts.Intents.Insert.EMAIL_TYPE, Contacts.ContactMethodsColumns.TYPE_WORK);
startActivity(it);
這是最通用的分享 Intent,會列出裝置上所有能接收純文字輸出的應用,如簡訊、Email、社群軟體等:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_TEXT, "The email subject text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
android.intent.action.ALL_APPS android.intent.action.ANSWER android.intent.action.ATTACH_DATA android.intent.action.BUG_REPORT android.intent.action.CALL android.intent.action.CALL_BUTTON android.intent.action.CHOOSER android.intent.action.CREATE_LIVE_FOLDER android.intent.action.CREATE_SHORTCUT android.intent.action.DELETE android.intent.action.DIAL android.intent.action.EDIT android.intent.action.GET_CONTENT android.intent.action.INSERT android.intent.action.INSERT_OR_EDIT android.intent.action.MAIN android.intent.action.MEDIA_SEARCH android.intent.action.PICK android.intent.action.PICK_ACTIVITY android.intent.action.RINGTONE_PICKER android.intent.action.RUN android.intent.action.SEARCH android.intent.action.SEARCH_LONG_PRESS android.intent.action.SEND android.intent.action.SENDTO android.intent.action.SET_WALLPAPER android.intent.action.SYNC android.intent.action.SYSTEM_TUTORIAL android.intent.action.VIEW android.intent.action.VOICE_COMMAND android.intent.action.WEB_SEARCH android.net.wifi.PICK_WIFI_NETWORK
android.provider.Settings 類別提供了大量跳轉到系統各設定頁面的常數,使用方式很簡單:
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
若要直接進入行動網路設定,需要指定組件:
Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
intent.setComponent(new ComponentName("com.android.phone", "com.android.phone.Settings"));
startActivity(intent);
// 進入網路營運商頁面
Intent intent2 = new Intent(Intent.ACTION_MAIN);
intent2.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
startActivity(intent2);
常用常數對照如下:
android.settings.AIRPLANE_MODE_SETTINGS android.settings.APN_SETTINGS android.settings.APPLICATION_DEVELOPMENT_SETTINGS android.settings.APPLICATION_SETTINGS android.settings.BLUETOOTH_SETTINGS android.settings.DATA_ROAMING_SETTINGS android.settings.DATE_SETTINGS android.settings.DISPLAY_SETTINGS android.settings.INPUT_METHOD_SETTINGS android.settings.INTERNAL_STORAGE_SETTINGS android.settings.LOCALE_SETTINGS android.settings.LOCATION_SOURCE_SETTINGS android.settings.MANAGE_APPLICATIONS_SETTINGS android.settings.MEMORY_CARD_SETTINGS android.settings.NETWORK_OPERATOR_SETTINGS android.settings.QUICK_LAUNCH_SETTINGS android.settings.SECURITY_SETTINGS android.settings.SETTINGS android.settings.SOUND_SETTINGS android.settings.SYNC_SETTINGS android.settings.USER_DICTIONARY_SETTINGS android.settings.WIFI_IP_SETTINGS android.settings.WIFI_SETTINGS android.settings.WIRELESS_SETTINGS
Settings 類別中定義的常數還有更細的分類,例如:
Android 自定義 View 打造 QQ 音樂風格旋轉唱片
在音樂播放頁中,旋轉唱片是相當有辨識度的視覺元素。本文說明如何自定義 View,畫出圓形圖片,並透過 Handler 讓它持續旋轉。 測量與繪製圓形圖片 首先要在 onM
Android Studio 安裝與環境配置全流程筆記
Android Studio 的安裝本身不難,真正容易卡住的是後面幾個配置環節:緩存目錄、Gradle 離線包、以及主題與字型。這篇把整個流程按順序走一遍,並把容易踩的坑
自定義 View 的兩個隱形陷阱:merge 標籤與構造函數
在 Android 裡自定義 View,本質上不複雜:繼承合適的父類,重載需要的方法。即便如此,實際編碼時還是容易踩坑。本文總結兩個我親身遇到過的問題,希望能幫你少走彎路
開源元件 GridViewWithHeaderAndFooter 使用與原始碼解析
GridViewWithHeaderAndFooter 這個元件,讓你能像使用 ListView 一樣,為 GridView 加上 Header 與 Footer。專案網