Android開發系列3——Intent詳解

前言

  Intent:從字面上的意思是:意圖;目的;含義;目標。但在Android的開發過程中卻扮演了一個非常重要的角色。

一、Intent詳解

深入的理解Intent,可以查看Android系統下Intent類的詳細介紹:

// 僅僅截取部分代碼,分別介紹幾種Intent屬性

/**
 * An intent is an abstract description of an operation to be performed.  It
 * can be used with {@link Context#startActivity(Intent) startActivity} to
 * launch an {@link android.app.Activity},
 * {@link android.content.Context#sendBroadcast(Intent) broadcastIntent} to
 * send it to any interested {@link BroadcastReceiver BroadcastReceiver} components,
 * and {@link android.content.Context#startService} or
 * {@link android.content.Context#bindService} to communicate with a
 * background {@link android.app.Service}.
 *
 * <p>An Intent provides a facility for performing late runtime binding between the code in
 * different applications. Its most significant use is in the launching of activities, where it
 * can be thought of as the glue between activities. It is basically a passive data structure
 * holding an abstract description of an action to be performed.</p>
*/

// 中間省去很多內容,如果感興趣可以自己去Intent類文件詳細研究。

public class Intent implements Parcelable, Cloneable{

 	private String mAction;
    private Uri mData;
    private String mType;
    private String mIdentifier;
    private String mPackage;
    private ComponentName mComponent;
    private int mFlags;
    private ArraySet<String> mCategories;
    @UnsupportedAppUsage
    private Bundle mExtras;
    private Rect mSourceBounds;
    private Intent mSelector;
    private ClipData mClipData;
    private int mContentUserHint = UserHandle.USER_CURRENT;
    /** Token to track instant app launches. Local only; do not copy cross-process. */
    private String mLaunchToken;

可以通過Intent類文件瞭解Intent的屬性,以及Intent的使用描述。

一、Intent

  Intent是一個消息傳遞對象,可以通過多種方式促進組件之間的通信,也可以用於從其他應用組件操作。總結其用法主要包括以下三個:

  • 啓動Activity

  Activity通過將Intent傳遞給startActivity()啓動一個新的Activity實例,同時Intent由於描述啓動Activity,並攜帶任何必要的數據。具體啓動Activity的例子可以參看:Android開發系列2——Activity頁面跳轉詳解的例子。

  • 啓動服務

  Service 是一個不使用用戶界面而在後臺執行操作的組件,可以使用 Service 類的方法來啓動服務。通過將 Intent 傳遞給 startService(),您可以啓動服務執行一次性操作(例如,下載文件)。Intent 用於描述要啓動的服務,並攜帶任何必要的數據。

  • 傳遞廣播

  廣播是任何應用均可接收的消息。系統將針對系統事件(例如:系統啓動或設備開始充電時)傳遞各種廣播。通過將 Intent 傳遞給 sendBroadcast() 或 sendOrderedBroadcast(),您可以將廣播傳遞給其他應用。

二、Intent 類型

Intent存在兩種類型:

  • 顯式Intent: 通過提供目標應用的軟件包名稱或完全限定的組件類名來指定可處理 Intent 的應用。通常,您會在自己的應用中使用顯式 Intent 來啓動組件,這是因爲您知道要啓動的 Activity 或服務的類名。例如,您可能會啓動您應用內的新 Activity 以響應用戶操作,或者啓動服務以在後臺下載文件。

  • 隱式Intent: 不會指定特定的組件,而是聲明要執行的常規操作,從而允許其他應用中的組件來處理。例如,如需在地圖上向用戶顯示位置,則可以使用隱式 Intent,請求另一具有此功能的應用在地圖上顯示指定的位置。

隱式Intent
隱式 Intent 如何通過系統傳遞以啓動其他 Activity:[1] Activity A 創建包含操作描述的 Intent,並將其傳遞給 startActivity()。[2] Android 系統搜索所有應用中與 Intent 匹配的 Intent 過濾器。找到匹配項之後,[3] 該系統通過調用匹配 Activity (Activity B) 的 onCreate() 方法並將其傳遞給 Intent,以此啓動匹配 Activity。

三、Intent詳細介紹

Intent:對象攜帶Android系統用來確定要啓動那些組件的信息(例如:準確的組件名稱或應當接收該Intent的組件類別),以及收件人組件爲了正確執行操作而使用的信息(例如:要採取的操作以及要處理的數據)。
Intent對象內包括7大屬性:
  1)Component(組件)
  2)Action (動作)
  3)Category(類別)
  4)Data(數據)
  5) Type(數據類型)
  6)Extras(擴展信息)
  7)Flags(標誌位)
接下來詳細介紹一下7大屬性,每種屬性怎麼使用。

2.1 Component(組件)

Component其實就是Intent啓動的組件名稱。(這個是可選項)

Intent在使用過程中,常用的方法:setComponent()、setClass()、setClassName()、或Inent()構建函數等,這些方法最終都是設置ComponentName。

2.3 Action (動作)

從字面意思就可以理解Action的作用,Action是一個抽象動作(方法),這個動作具體由那個組件(或者是Activity,或者是BroadCastReceived)來完成,Action並不關心,Action僅僅只是一個字符串

Android系統提供了大量的Action常量,接下來列舉一些常用的:

Action常量 對應字符串 簡要說明
ACTION_MAIN android.intent.action.MAIN 應用程序入口
ACTION_VIEW android.intent.action.VIEW 查看指定數據
ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA 指定某塊數據將被附加到其他地方
ACTION_EDIT android.intent.action.EDIT 編輯指定數據
ACTION_PICK android.intent.action.PICK 從列表中選擇某項,並返回所選的數據
ACTION_CHOOSER android.intent.action.CHOOSER 顯示一個Activity選擇器
ACTION_GET_CONTENT android.intent.action.GET_CONTENT 讓用戶選擇數據,並返回所選數據
ACTION_DIAL android.intent.action.DIAL 顯示撥號面板
ACTION_CALL android.intent.action.CALL 直接向之指定用戶打電話
ACTION_SEND android.intent.action.SEND 向其他人發送數據
ACTION_SENDTO android.intent.action.SENDTO 向其他人發送消息
ACTION_ANSWER android.intent.action.ANSWER 應答電話
ACTION_INSERT android.intent.action.INSERT 插入數據
ACTION_DELETE android.intent.action.DELETE 刪除數據
ACTION_RUN android.intent.action.RUN 運行數據
ACTION_SYNC android.intent.action.SYNC 執行數據同步
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 用於選擇Activity
ACTION_SEARCH android.intent.action.SEARCH 執行搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 執行Web搜索
ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST 工廠測試的入口點
2.3 Category (類別)

一個包含處理Intent組件類型的附加信息的字符串。可以將任意數量的類別描述放入一個Intent中,但大多數Intent都不需要類別。

Category常量 對應字符串 簡單說明
CATEGORY_DEFAULT android.intent.category.DEFAULT 默認的Category
CATEGORY_BROWSABLE android.intent.category.BROWSABLE 指定該Activity能被瀏覽器安全調用
CATEGORY_TAB android.intent.category.TAB 指定Activity作爲TabActivity的Tab頁
CATEGORY_LAUNCHER android.intent.category.LAUNCHER Activity顯示頂級程序列表
CATEGORY_INFO android.intent.category.INFO 用於提供包信息
CATEGORY_HOME android.intent.category.HOME 設置該Activity隨系統啓動而運行
CATEGORY_TEST android.intent.category.TEST 該Activity是一個測試
CATEGORY_PREFERENCE android.intent.category.PREFERENCE 該Activity是參數面板
CATEGORY_CAR_DOCK android.intent.category.CAR_DOCK 指定手機被插入汽車底座(硬件)時運行該Activity
CATEGORY_DESK_DOCK android.intent.category.DESK_DOCK 指定手機被插入桌面底座(硬件)時運行該Activity
CATEGORY_CAR_MODE android.intent.category.CAR_MODE 設置該Activity可在車載環境下使用
2.4 Data (數據)

  引用待操作數據和或該數據MIME類型的URI;data的類型由Intent的action決定。

2.5 Type(數據類型)

  Type是Data數據的類型,Type的設置需要和Data配合使用。
  要僅設置數據 URI,請調用 setData()。要僅設置 MIME 類型,請調用 setType()。

2.6 Extra(擴展信息)

  Extra 攜帶完成請求操作所需的附加信息的鍵值對。正如某些操作使用特定類型的數據 URI 一樣,有些操作也使用特定的 extra。

  可以使用各種 putExtra() 方法添加 extra 數據,每種方法均接受兩個參數:鍵名和值。還可以創建一個包含所有 extra 數據的 Bundle 對象,然後使用 putExtras() 將 Bundle 插入 Intent 中。同時可以通過getExtra()方法獲取extra數據。

類型 作用
EXTRA_BCC 存放郵件密送人地址的字符串數組
EXTRA_CC 存放郵件抄送人地址的字符串數組
EXTRA_EMAIL 存放郵件地址的字符串數組
EXTRA_SUBJECT 存放郵件主題字符串
EXTRA_TEXT 存放郵件內容
EXTRA_KEY_EVENT 以KeyEvent對象方式存放觸發Intent 的按鍵
EXTRA_PHONE_ NUMBER 存放調用ACTION_CALL 時的電話號碼
2.7 Flags(標誌位)

  標誌在 Intent 類中定義,充當 Intent 的元數據。標誌可以指示 Android 系統如何啓動 Activity(例如,Activity 應屬於哪個任務),以及啓動之後如何處理(例如,Activity 是否屬於最近的 Activity 列表),可以通過setFlags()或者addFlags()可以把標籤flag用在Intent中。

類型 作用
FLAG_ACTIVITY_CLEAR_TOP 相當於SingleTask
FLAGE_ACTIVITY_SINGLE_TOP 相當於SingleTop
FLAG_ACTIVITY_NEW_TASK 類似於SingleInstance
FLAG_ACTIVITY_NO_HISTORY 當離開該Activity後,該Activity將被從任務棧中移除

總結

在Android中扮演一箇中間層的角色,三大組件Activity、Service、BroadcastReceiver都需要Intent啓動,組件需要通過Intent進行啓動、數據傳遞和跳轉,可以參考Android開發系列的Activity、Service、BroadcastReceiver等文章,查看使用Intent。

持續更新中……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章