關於Intent的七大屬性

http://blog.csdn.net/u012702547/article/details/50178429

原諒我愚昧,Intent七大屬性這個概念我也是昨天才接觸到,看了一下,都是一些常用的東西,就是沒有總結過,那麼今天就來簡單總結一下。

Intent七大屬性是指Intent的ComponentName、Action、Category、Data、Type、Extra以及Flag,七個屬性,總體上可以分爲3類:

第一類:啓動,有ComponentName(顯式),Action(隱式),Category(隱式)。

第二類:傳值,有Data(隱式),Type(隱式),Extra(隱式、顯式)。

第三類:啓動模式,有Flag。

下面我們逐一來說。

1.ComponentName

Component本身有組件的意思,我們通過設置Component可以啓動其他的Activity或者其他應用中的Activity,來看一個簡單的實例:

啓動同一個App中另外一個Activity:

  1. intent = new Intent();  
  2.             intent.setComponent(new ComponentName(this, SecondActivity.class));  
  3.             startActivity(intent);  
這中啓動方式等同於以下兩種啓動方式:

  1. intent = new Intent(this,SecondActivity.class);  
  2.             startActivity(intent);  

  1. intent = new Intent();  
  2.             intent.setClass(this, SecondActivity.class);  
  3.             startActivity(intent);  
當然,通過設置ComponentName屬性我們也可以啓動其他App中的Activity,關於這一塊的內容大家可以參考關於ComponentName的使用。下面我們看看隱式啓動。

2.Action和Category

因爲在實際開發中,Action大多時候都是和Category一起使用的,所以這裏我們將這兩個放在一起來講解。Intent中的Action我們在使用廣播的時候用的比較多,在Activity中,我們可以通過設置Action來隱式的啓動一個Activity,比如我們有一個ThirdActivity,我們在清單文件中做如下配置:

  1. <activity  
  2.     android:name=".ThirdActivity"  
  3.     android:label="@string/title_activity_third" >  
  4.     <intent-filter>  
  5.         <category android:name="android.intent.category.DEFAULT" />  
  6.   
  7.   
  8.         <action android:name="com.qf.ThirdActivity" />  
  9.     </intent-filter>  
  10. </activity>  
當我們在清單文件中做了這樣的配置之後,我們的ThirdActivity會就會響應這個動作,怎麼那麼怎麼響應呢?看下面:

  1. intent = new Intent();  
  2. intent.setAction("com.qf.ThirdActivity");  
  3. startActivity(intent);  
當然,我們也可以寫的更簡單一些,如下:

  1. intent = new Intent("com.qf.ThirdActivity");  
  2.             startActivity(intent);  
通過這中方式我們也可以啓動一個Activity,那麼大家可能也注意到了,我們的清單文件中有一個category的節點,那麼沒有這個節點可以嗎?不可以!!當我們使用這種隱式啓動的方式來啓動一個Activity的時候,必須要action和category都匹配上了,該Activity纔會成功啓動。如果我們沒有定義category,那麼可以暫時先使用系統默認的category,總之,category不能沒有。這個時候我們可能會有疑問了,如果我有多個Activity都配置了相同的action,那麼會啓動哪個?看看下面這個熟悉的圖片:

當我們有多個Activity配置了相同的action的時候,那麼系統會彈出來一個選擇框,讓我們自己選擇要啓動那個Activity。

action我們只能添加一個,但是category卻可以添加多個(至少有一個,沒有就要設置爲DEFAULT),如下:

  1. <activity  
  2.     android:name=".ThirdActivity"  
  3.     android:label="@string/title_activity_third" >  
  4.     <intent-filter>  
  5.         <category android:name="android.intent.category.DEFAULT" />  
  6.         <category android:name="mycategory" />  
  7.   
  8.         <action android:name="com.qf.ThirdActivity" />  
  9.     </intent-filter>  
  10. </activity>  
相應的我們的啓動方式也可以修改,如下:

  1. intent = new Intent("com.qf.ThirdActivity");  
  2.             intent.addCategory("mycategory");  
  3.             startActivity(intent);  

3.Data

通過設置data,我們可以執行打電話,發短信,開發網頁等等操作。究竟做哪種操作,要看我們的數據格式:

  1. // 打開網頁  
  2. intent = new Intent(Intent.ACTION_VIEW);  
  3. intent.setData(Uri.parse("http://www.baidu.com"));  
  4. startActivity(intent);  
  5. // 打電話  
  6. intent = new Intent(Intent.ACTION_VIEW);  
  7. intent.setData(Uri.parse("tel:18565554482"));  
  8. startActivity(intent);  
當我們的data是一個http協議的時候,系統會自動去查找可以打開http協議的Activity,這個時候如果手機安裝了多個瀏覽器,那麼系統會彈出多個瀏覽器供我們選擇。這是我們通過設置Data來啓動一個Activity,同時,我們也可以通過設置一個Data屬性來將我們的Activity發佈出去供別人調用,怎麼發佈呢?

  1. <activity  
  2.     android:name=".HttpActivity"  
  3.     android:label="@string/title_activity_http" >  
  4.     <intent-filter>  
  5.         <action android:name="android.intent.action.VIEW" />  
  6.   
  7.         <category android:name="android.intent.category.DEFAULT" />  
  8.   
  9.         <data  
  10.             android:scheme="http" />  
  11.     </intent-filter>  
  12. </activity>  

在data節點中我們設置我們這個Activity可以打開的協議,我們這裏設置爲http協議,那麼以後要打開一個http請求的時候,系統都會讓我們選擇是否用這個Activity打開。當然,我們也可以自己定義一個協議(自己定義的協議,由於別人不知道,所以只能由我們自己的程序打開)。比如下面這樣:

  1. <activity  
  2.     android:name=".HttpActivity"  
  3.     android:label="@string/title_activity_http" >  
  4.     <intent-filter>  
  5.         <action android:name="android.intent.action.VIEW" />  
  6.   
  7.         <category android:name="android.intent.category.DEFAULT" />  
  8.   
  9.         <data  
  10.             android:scheme="myhttp" />  
  11.     </intent-filter>  
  12. </activity>  
那麼我們怎麼打開自己的Activity呢?

  1. intent = new Intent();  
  2.             intent.setData(Uri.parse("myhttp://www.baidu.com"));  
  3.             startActivity(intent);  
這個例子沒有什麼實際意義,我只是舉一個自定義協議的栗子。

其實,說到這裏,大家應該明白了爲什麼我們說data是隱式傳值,比如我們打開一個網頁,http協議後面跟的就是網頁地址,我們不用再單獨指定要打開哪個網頁。

4.Type

type的存在,主要是爲了對data的類型做進一步的說明,但是一般情況下,只有data屬性爲null的時候,type屬性纔有效,如果data屬性不爲null,系統會自動根據data中的協議來分析data的數據類型,而不會去管type,我們先來看看下面一段源碼:

  1. /** 
  2.  * Set the data this intent is operating on.  This method automatically 
  3.  * clears any type that was previously set by {@link #setType} or 
  4.  * {@link #setTypeAndNormalize}. 
  5.  * 
  6.  * <p><em>Note: scheme matching in the Android framework is 
  7.  * case-sensitive, unlike the formal RFC. As a result, 
  8.  * you should always write your Uri with a lower case scheme, 
  9.  * or use {@link Uri#normalizeScheme} or 
  10.  * {@link #setDataAndNormalize} 
  11.  * to ensure that the scheme is converted to lower case.</em> 
  12.  * 
  13.  * @param data The Uri of the data this intent is now targeting. 
  14.  * 
  15.  * @return Returns the same Intent object, for chaining multiple calls 
  16.  * into a single statement. 
  17.  * 
  18.  * @see #getData 
  19.  * @see #setDataAndNormalize 
  20.  * @see android.net.Uri#normalizeScheme() 
  21.  */  
  22. public Intent setData(Uri data) {  
  23.     mData = data;  
  24.     mType = null;  
  25.     return this;  
  26. }  
  27.   
  28. /** 
  29.  * Set an explicit MIME data type. 
  30.  * 
  31.  * <p>This is used to create intents that only specify a type and not data, 
  32.  * for example to indicate the type of data to return. 
  33.  * 
  34.  * <p>This method automatically clears any data that was 
  35.  * previously set (for example by {@link #setData}). 
  36.  * 
  37.  * <p><em>Note: MIME type matching in the Android framework is 
  38.  * case-sensitive, unlike formal RFC MIME types.  As a result, 
  39.  * you should always write your MIME types with lower case letters, 
  40.  * or use {@link #normalizeMimeType} or {@link #setTypeAndNormalize} 
  41.  * to ensure that it is converted to lower case.</em> 
  42.  * 
  43.  * @param type The MIME type of the data being handled by this intent. 
  44.  * 
  45.  * @return Returns the same Intent object, for chaining multiple calls 
  46.  * into a single statement. 
  47.  * 
  48.  * @see #getType 
  49.  * @see #setTypeAndNormalize 
  50.  * @see #setDataAndType 
  51.  * @see #normalizeMimeType 
  52.  */  
  53. public Intent setType(String type) {  
  54.     mData = null;  
  55.     mType = type;  
  56.     return this;  
  57. }  

當我們設置data的時候,系統會默認將type設置爲null,當我們設置type的時候,系統會默認將data設置爲null.也就是說,一般情況下,data和type我們只需要設置一個就行了,如果我們既想要設置data又想要設置type,那麼可以使用
  1. setDataAndType(Uri data, String type)  

這個方法來完成。下面我們來看看通過給Intent設置type來打開一個音樂播放器。代碼如下:

  1. intent = new Intent();  
  2.             intent.setAction(Intent.ACTION_VIEW);  
  3.             Uri data = Uri.parse("file:///storage/emulated/0/xiami/audios/被動.mp3");  
  4.             intent.setDataAndType(data, "audio/mp3");  
  5.             startActivity(intent);  
如果我們要打開的是視頻文件,那麼type就要設置爲"video/*",其中*表示支持所有的視頻文件。

5.Extra

Extra就比較好理解了,我們經常使用它來在Activity之間傳遞數據,Extra可以傳遞基本類型,String類型以及實現了Serializable或者Parcelable接口的類,具體用法不多說。

6.Flag

通過設置Flag,我們可以設定一個Activity的啓動模式,這個和launchMode基本上是一樣的,所以我也不再細說,關於launchMode的使用參見launchMode使用詳解

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