Intent

一.Intent的介紹
Intent的中文意思是“意圖,意向”,在Android中提供了Intent機制來協助應用間的交互與通訊,Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent不僅可用於應用程序之間,也可用於應用程序內部的Activity/Service之間的交互。因此,可以將Intent理解爲不同組件之間通信的“媒介”專門提供組件互相調用的相關信息。
二.Inten啓動組件的方法
Intent可以啓動一個Activity,也可以啓動一個Service,還可以發起一個廣播Broadcasts。具體方法如下:這裏寫圖片描述
三.Intent的屬性
Intent有以下幾個屬性:
動作(Action),數據(Data),分類(Category),類型(Type),組件(Compent)以及擴展信(Extra)。其中最常用的是Action屬性和Data屬性。
1.Intent的Action屬性
Action是指Intent要完成的動作,是一個字符串常量。SDK中定義了一些標準的Action常量如下表所示。

(1.)ACTION_DIAL
Added in API level 1

Activity Action: Dial a number as specified by the data. This shows a UI with the number being dialed, allowing the user to explicitly initiate the call.

Input: If nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.

Output: nothing.
Constant Value: “android.intent.action.DIAL”
(2.)ACTION_VIEW

Activity Action: Display the data to the user. This is the most common action performed on data – it is the generic action you can use on a piece of data to get the most reasonable thing to occur. For example, when used on a contacts entry it will view the entry; when used on a mailto: URI it will bring up a compose window filled with the information supplied by the URI; when used with a tel: URI it will invoke the dialer.

Input: getData() is URI from which to retrieve data.

Output: nothing.
Constant Value: “android.intent.action.VIEW”
(3.)ACTION_CALL

Activity Action: Perform a call to someone specified by the data.

Input: If nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.

Output: nothing.

Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.

Note: this Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL, however.
Constant Value: “android.intent.action.CALL”
(4.)ACTION_SENDTO

Activity Action: Send a message to someone specified by the data.

Input: getData() is URI describing the target.

Output: nothing.
Constant Value: “android.intent.action.SENDTO”
2.Intent的Data屬性
Intent的Data屬性是執行動作的URI和MIME類型,不同的Action有不同的Data數據指定。比如:ACTION_EDIT Action應該和要編輯的文檔URI Data匹配,ACTION_VIEW應用應該和要顯示的URI匹配。
3.Intent的Category屬性
Intent中的Category屬性是一個執行動作Action的附加信息。比如:CATEGORY_HOME則表示放回到Home界面,ALTERNATIVE_CATEGORY表示當前的Intent是一系列的可選動作中的一個。下表是SDK文檔中關於Category的信息。
Constant
Meaning
CATEGORY_BROWSABLE
The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.
CATEGORY_GADGET
The activity can be embedded inside of another activity that hosts gadgets.
CATEGORY_HOME
The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.
CATEGORY_LAUNCHER
The activity can be the initial activity of a task and is listed in the top-level application launcher.
CATEGORY_PREFERENCE
The target activity is a preference panel.
4.Intent的Type屬性
Intent的Type屬性顯式指定Intent的數據類型(MIME)。一般Intent的數據類型能夠根據數據本身進行判定,但是通過設置這個屬性,可以強制採用顯式指定的類型而不再進行推導。

5.Intent的Compent屬性
Intent的Compent屬性指定Intent的的目標組件的類名稱。通常 Android會根據Intent 中包含的其它屬性的信息,比如action、data/type、category進行查找,最終找到一個與之匹配的目標組件。但是,如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再執行上述查找過程。指定了這個屬性以後,Intent的其它所有屬性都是可選的。
6.Intent的Extra屬性
Intent的Extra屬性是添加一些組件的附加信息。比如,如果我們要通過一個Activity來發送一個Email,就可以通過Extra屬性來添加subject和body。
下面的例子在第一個Activity的EditText輸入用戶名,該年齡保存在Intent的Extras屬性中。當單擊Button時,會在第二個Activity中顯示用戶名。

發佈了59 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章