Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android Intent 的各種用法大整理

編輯:開發入門

Intent 是 Android 特有的元件溝通機制。你可以在 Intent 中指定程式要執行的動作(例如 view、edit、dial)以及該動作所需的資料,指定好之後只要呼叫 startActivity(),系統便會自動找出最符合條件的應用程式並執行它。以下把實務上常用到的寫法整理成一份清單。

從 BroadcastReceiver 啟動 Activity 別忘了加 flag

BroadcastReceiver 沒有 Activity 的任務堆疊,因此從它啟動新的 Activity 時,必須明確加上 FLAG_ACTIVITY_NEW_TASK,否則會直接拋出例外:

public class MyReceiver extends BroadcastReceiver {
    public static final String action = "acc";

    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, Receivered.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

Intent 的三種指定方式

一、只指定 action 與 type

// SIM 匯入
Intent importIntent = new Intent(Intent.ACTION_VIEW);
importIntent.setType("vnd.android.cursor.item/sim-contact");
importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
menu.add(0, 0, 0, R.string.importFromSim)
    .setIcon(R.drawable.ic_menu_import_contact)
    .setIntent(importIntent);

二、指定 action、data 與 type(隱式查找)

// uri: content://simcontacts/simPeople/(id)
Intent intent = new Intent("android.intent.action.SIMEDIT", uri);
startActivity(intent);

程式會依 data 中的 uri 去尋找對應的 type,這一步由 ContentProvider 的 getType() 提供:

case SIM_PEOPLE_ID:
    return "vnd.android.cursor.item/sim-contact";

並在 AndroidManifest.xml 中以 intent-filter 比對:

<intent-filter>
    <action android:name="android.intent.action.SIMEDIT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="vnd.android.cursor.item/sim-contact" />
</intent-filter>

若想自己指定 type,只能使用 setDataAndType(),因為 data 與 type 必須同時設定才不會互相覆蓋。

三、其他設定 Intent 屬性的方法

Intent setComponent(ComponentName component)
Intent setClassName(Context packageContext, String className)
Intent setClassName(String packageName, String className)
Intent setClass(Context packageContext, Class cls)

常見動作用的 Intent 範例

開啟網頁

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

顯示地圖與規劃路線

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

要做路線規劃,則把起訖座標組進 Google Maps 的網址再交給 ACTION_VIEW 即可。

撥打電話

// 只開啟撥號面板,由使用者按下通話鍵
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);

// 直接撥出,必須在 manifest 加入 CALL_PHONE 權限
Uri uri2 = Uri.parse("tel:xxxxxx");
Intent it2 = new Intent(Intent.ACTION_CALL, uri2);
startActivity(it2);

傳送簡訊與多媒體訊息

// 開啟簡訊撰寫畫面
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 uri3 = Uri.parse("content://media/external/images/media/23");
Intent it3 = new Intent(Intent.ACTION_SEND);
it3.putExtra("sms_body", "some text");
it3.putExtra(Intent.EXTRA_STREAM, uri3);
it3.setType("image/png");
startActivity(it3);

寄送 Email

// 最簡單的方式
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);

// 帶入主旨與本文,並讓使用者挑選郵件程式
Intent it2 = new Intent(Intent.ACTION_SEND);
it2.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it2.putExtra(Intent.EXTRA_TEXT, "The email body text");
it2.setType("text/plain");
startActivity(Intent.createChooser(it2, "Choose Email Client"));

// 多收件者、副本、主旨
Intent it3 = new Intent(Intent.ACTION_SEND);
String[] tos = {"me@abc.com"};
String[] ccs = {"you@abc.com"};
it3.putExtra(Intent.EXTRA_EMAIL, tos);
it3.putExtra(Intent.EXTRA_CC, ccs);
it3.putExtra(Intent.EXTRA_TEXT, "The email body text");
it3.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it3.setType("message/rfc822");
startActivity(Intent.createChooser(it3, "Choose Email Client"));

// 加上附件
Intent it4 = new Intent(Intent.ACTION_SEND);
it4.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it4.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
it4.setType("audio/mp3");
startActivity(Intent.createChooser(it4, "Choose Email Client"));

搭配 createChooser 的好處是:系統會列出所有能處理該 type 的程式讓使用者選擇,避免你的 App 與某個特定郵件客戶端強綁在一起。

播放多媒體

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

// 播放媒體庫中的指定項目
Uri uri2 = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(it2);

移除與安裝應用程式

// 解除安裝
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);

搜尋應用市集

// 依套件名稱搜尋,pkg_name 是應用的完整套件路徑
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

// 開啟指定應用的詳細頁面
Uri uri2 = Uri.parse("market://details?id=app_id");
Intent it2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(it2);

小結

Intent 的用法可以歸納成兩條路:一是明確指定元件(setClass、setClassName、setComponent),適合跳轉自己 App 內的畫面;二是隱式宣告動作與資料型別(action + data + type),交給系統去媒合。後者正是 Android 鬆耦合設計的精髓——你的程式不需要知道撥號程式叫什麼名字,只要說清楚「我要打電話給這個號碼」就好。實務上搭配 createChooser,還能把選擇權交還給使用者,讓應用之間的互補關係更自然。

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