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

跨應用啟動 Activity 與 Intent Action 速查大全

編輯:關於 Android 程式設計

Intent 是 Android 組件之間溝通的核心。這一篇整理如何從自己的應用啟動別人的 Activity、如何自訂 action 讓外部來呼叫,以及常用系統 action 與設定頁面的對照表。

一、啟動其他應用的 Activity

寫法一:指定 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

二、自訂 action 讓外部啟動你的 Activity

先在被呼叫的那個應用的 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);

三、常見 Intent 用法速查

網頁與搜尋

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

多媒體簡訊(MMS)

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

四、Intent Action 對照表

  • ACTION_MAIN:主要進入點,不期待接收資料。
  • ACTION_VIEW:向使用者顯示指定資料。
  • ACTION_ATTACH_DATA:把某些資料附加到別處,例如將圖片設為聯絡人頭像。
  • ACTION_EDIT:開啟既有資料供明確編輯。
  • ACTION_PICK:從資料集合中挑一項並回傳。
  • ACTION_CHOOSER:顯示選擇器,讓使用者決定由哪個應用接手。
  • ACTION_GET_CONTENT:讓使用者選擇特定類型資料並回傳,例如拍照或錄音。
  • ACTION_DIAL:撥號介面,顯示號碼並交由使用者確認發起通話。
  • ACTION_CALL:直接依資料撥號。此 action 在應用內啟動通話有缺陷,多數情況應改用 ACTION_DIAL;緊急號碼也只能用 ACTION_DIAL。
  • ACTION_SEND:傳遞資料,接收方由使用者決定。
  • ACTION_SENDTO:傳送訊息給指定對象。
  • ACTION_ANSWER:接聽來電。
  • ACTION_INSERT:在容器中插入一筆空白項目。
  • ACTION_DELETE:從容器中刪除指定資料。
  • ACTION_RUN:執行資料。
  • ACTION_SYNC:同步資料。
  • ACTION_PICK_ACTIVITY:為已知 Intent 挑選 Activity 並回傳所選類別。
  • ACTION_SEARCH:執行搜尋。
  • ACTION_WEB_SEARCH:執行網路搜尋。
  • ACTION_FACTORY_TEST:出廠測試的主要進入點。

五、標準廣播 Action

  • ACTION_TIME_TICK:目前時間改變,每分鐘發送一次。無法透過組件聲明接收,只能用 Context.registerReceiver() 註冊。
  • ACTION_TIME_CHANGED:時間被設定。
  • ACTION_TIMEZONE_CHANGED:時區改變。
  • ACTION_BOOT_COMPLETED:系統完成開機後發送一次。
  • ACTION_PACKAGE_ADDED:新應用包已安裝,資料含套件名稱(剛安裝的程式收不到這個廣播)。
  • ACTION_PACKAGE_CHANGED:既有應用包已變更,資料含套件名稱。
  • ACTION_PACKAGE_REMOVED:應用包已移除,資料含套件名稱(正在移除的程式收不到)。
  • ACTION_PACKAGE_RESTARTED:使用者重啟某套件,其所有行程被結束,相關執行期狀態應清除(重啟中的程式收不到)。
  • ACTION_PACKAGE_DATA_CLEARED:使用者已清除某套件的資料(該程式收不到)。
  • ACTION_BATTERY_CHANGED:電池狀態或電量改變。無法透過組件聲明接收,只能用 Context.registerReceiver() 註冊。
  • ACTION_UID_REMOVED:某個使用者 ID 已從系統移除。

六、常用 action 常數一覽

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 類別中定義的常數還有更細的分類,例如:

  • ACTION_ACCESSIBILITY_SETTINGS:無障礙模組設定。
  • ACTION_ADD_ACCOUNT:新增帳戶畫面。
  • ACTION_APPLICATION_DETAILS_SETTINGS:特定應用的詳細資訊頁。
  • ACTION_DEVICE_INFO_SETTINGS:裝置資訊(序號、軟體版本、電話號碼等)。
  • ACTION_INPUT_METHOD_SUBTYPE_SETTINGS:啟用或停用輸入法子類型。
  • ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS:管理所有應用。
  • ACTION_PRIVACY_SETTINGS:隱私選項設定。
  • ACTION_SEARCH_SETTINGS:全域搜尋設定。
  • ACTION_WIFI_IP_SETTINGS:設定 Wi-Fi 靜態 IP。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved