Android 中的 Intent 物件

































Intent 可以被想像成 Android 當中的事件 (Event),用來串連 Android 中的 Activity 與 Service 等元件。每當一個 Activity 想執行一個操作時,會創建出一個 intent,該 intent 描述了 Activity 想要做甚麼事、想處理什麼類型的資料等。

Android 會比較 Intent 與 AndroidManifest.xml 中的 <intent-filter> 內容,以尋找最合適的 Activity 來處理回應 Intent 的請求。

根據 Google 的說明,Intent 是一次操作的抽象描述,這句話實在太玄了。我們可以解釋成,Intent 代表一個跨越 Activity 或 Service 的呼叫,Intent 當中會負責攜帶這次操作所需要的資料,並指定操作的名稱或類型等。

Intent 是用來啟動 Activity、Service 與 Broadcast-Receiver 的物件,Intent 中包含了要傳給待啟動者的訊息,並用非同步模式 (Asynchronous) 傳給對方。對於 Activity 與 Service 而言,Intent 會指定動作名稱並傳送訊息所在的網址 (URI) 給對方。舉例而言,Intent 可能要求 Activity 呈現出圖片給使用者編輯。對於 Broadcast-Receiver 而言,Intent 會賦予該動作一個名稱,像是廣播拍照 (相機按下) 訊息給相關的物件。

Content providers are activated when they're targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.

有許多種啟動元件的方法如下,像是可以用 Context.startActivity() 或 Activity.startActivityForResult() 函數,被啟動的 Acitvity 可以透過 getIntent() 取得啟動它的物件。Android 會呼叫 onNewIntent() 方法去傳送它 (Intent???) 給後續的 Intent。

There are separate methods for activating each type of component:

An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity's onNewIntent() method to pass it any subsequent intents.

Activity 通常會啟動下一個 Activity,如果 Activity 希望取得某些執行結果 (回傳訊息),可以用 startActivityForResult() 而不是用 startActivity()。舉例而言,如果某個 Activity 啟動了一個讓使用者選取一張圖片的 Activity ,則可能會希望傳回被選取的圖片。這個結果可以透過 Intent 中的 onActivityResult() 完成。

One activity often starts the next one. If it expects a result back from the activity it's starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult() method.

您也可以透過傳送一個 Intent 給 Context.startService() 以啟動服務,Android 會啟動該服務的 onStart() 函數並傳送該 Intent 給他。

A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service's onStart() method and passes it the Intent object.

同樣的,Intent 也可以被傳送給 Context.bindService() 以建立來源物件與目標服務間的連結,Service 會在 onBind() 這個函數中收到該 Intent。(假如該服務尚未啟動,bindService() 可以選擇啟動它)。舉例而言,假如一個 Activity 透過 bindService() 建立了與音樂播放服務之間的連結,就能透過呼叫該服務的函數以控制音樂播放功能。

Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.

A later section, Remote procedure calls, has more details about binding to a service.

另外,一個應用也可以透過傳送 intent 給 Context.sendBroadcast(), Context.sendOrderedBroadcast(), 和 Context.sendStickyBroadcast() 等函數,以廣播訊息給相關的接收者 (Broadcast-Receiver),Android 會呼叫這些接收者的 onReceive() 函數以接收該訊息。

An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.

For more on intent messages, see the separate article, Intents and Intent Filters.

Intent 類別與函數

在 Intent 類別中,有好幾個建構函數,如下所示。

Intent() Create an empty intent.
Intent(Intent o) Copy constructor.
Intent(String action) Create an intent with a given action.
Intent(String action, Uri uri) Create an intent with a given action and for a given data url.
Intent(Context packageContext, Class<?> cls) Create an intent for a specific component.
Intent(String action, Uri uri, Context packageContext, Class<?> cls) Create an intent for a specific component with a specified action and data.

Intent 的 Action 主要是一些定義好了的常量,例如 PICK_ACTION,VIEW_ACTION,EDIT_ACTION 之類的,而 Data 則是一個 ContentURI 類型的變數。


  1. Android學習筆記(4)-學習Intent的使用 —


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License