顯式Intent和隱式Intent

來源:互聯網
上載者:User

標籤:android   blog   http   java   使用   os   io   strong   

http://blog.csdn.net/qs_csu/article/details/7995966

 

對於明確指出了目標組件名稱的Intent,我們稱之為“顯式Intent”。

對於沒有明確指出目標組件名稱的Intent,則稱之為“隱式 Intent”。


對於隱式意圖,Android是怎樣尋找到這個最合適的組件呢?記的前面我們在定義活動時,指定了一個intent-filter,Intent Filter(意圖過濾器)其實就是用來匹配隱式Intent的,當一個意圖對象被一個意圖過濾器進行匹配測試時,只有三個方面會被參考到:動作、資料(URI以及資料類型)和類別。

  動作測試(Action test)

  一個意圖對象只能指定一個動作名稱,而一個過濾器可能列舉多個動作名稱。如果意圖對象或過濾器沒有指定任何動作,結果將如下:

  • 如果過濾器沒有指定任何動作,那麼將阻塞所有的意圖,因此所有的意圖都會測試失敗。沒有意圖能夠通過這個過濾器。

  • 另一方面,只要過濾器包含至少一個動作,一個沒有指定動作的意圖對象自動通過這個測試

  類別測試(Category test)

  對於一個能夠通過類別匹配測試的意圖,意圖對象中的類別必須匹配過濾器中的類別。這個過濾器可以列舉另外的類別,但它不能遺漏在這個意圖中的任何類別。

  原則上一個沒有類別的意圖對象應該總能夠通過匹配測試,而不管過濾器裡有什麼。大部分情況下這個是對的。但有一個例外,Android把所有傳給 startActivity()的隱式意圖當作他們包含至少一個類別:"android.intent.category.DEFAULT" (CATEGORY_DEFAULT常量)。因此,想要接收隱式意圖的活動必須在它們的意圖過濾器中包含"android.intent.category.DEFAULT"。(帶"android.intent.action.MAIN" 和"android.intent.category.LAUNCHER"設定的過濾器是例外)

  資料測試(Data test)

  當一個意圖對象中的URI被用來和一個過濾器中的URI比較時,比較的是URI的各個組成部分。例如,如果過濾器僅指定了一個scheme,所有該scheme的URIs都能夠和這個過濾器相匹配;如果過濾器指定了一個scheme、主機名稱但沒有路經部分,所有具有相同scheme和主機名稱的 URIs都可以和這個過濾器相匹配,而不管它們的路經;如果過濾器指定了一個scheme、主機名稱和路經,只有具有相同scheme、主機名稱和路經的 URIs才可以和這個過濾器相匹配。當然,一個過濾器中的路徑規格可以包含萬用字元,這樣只需要部分匹配即可。

 

  資料測試同時比較意圖對象和過濾器中指定的URI和資料類型。規則如下:

  a. 一個既不包含URI也不包含資料類型的意圖對象僅在過濾器也同樣沒有指定任何URIs和資料類型的情況下才能通過測試。

  b. 一個包含URI但沒有資料類型的意圖對象僅在它的URI和一個同樣沒有指定資料類型的過濾器裡的URI匹配時才能通過測試。這通常發生在類似於mailto:和tel:這樣的URIs上:它們並不引用實際資料。

  c. 一個包含資料類型但不包含URI的意圖對象僅在這個過濾器列舉了同樣的資料類型而且也沒有指定一個URI的情況下才能通過測試。

  d. 一個同時包含URI和資料類型(或者可從URI推斷出資料類型)的意圖對象可以通過測試,如果它的類型和過濾器中列舉的類型相匹配的話。如果它的URI和這個過濾器中的一個URI相匹配或者它有一個內容content:或者檔案file: URI而且這個過濾器沒有指定一個URI,那麼它也能通過測試。換句話說,一個組件被假定為支援content:和file: 資料如果它的過濾器僅列舉了一個資料類型。

[java] view plaincopyprint?
  1. Java代碼public class MainActivity extends Activity  
  2. {  
  3.     /** Called when the activity is first created. */  
  4.     @Override       
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         Button button = (Button) this.findViewById(R.id.button);  
  11.         button.setOnClickListener(new View.OnClickListener()  
  12.         {  
  13.                         @Override  
  14.                         public void onClick(View v)  
  15.                         {  
  16.                                 Intent intent = new Intent();  
  17.                                 intent.setAction("com.hoo.kay");  
  18.                                 //設定資料URI與資料類型匹配  
  19.                                 //intent.setData(data);  
  20.                                 //intent.setType(type);注意這個方法會清除setData的內容,所以如果既要設定類型與資料,那麼使用setDataAndType  
  21.                                 intent.setDataAndType(Uri.parse("hoo://www.hoo.com/person"), "image/gif");  
  22.                                 startActivity(intent);  
  23.                         }  
  24.                 });  
  25.     }  
  26. }  

 

[java] view plaincopyprint?
  1. <application android:icon="@drawable/icon" android:label="@string/app_name">  
  2.         <activity android:name=".MainActivity"  
  3.                   android:label="@string/app_name">  
  4.             <intent-filter>  
  5.                 <action android:name="android.intent.action.MAIN" />  
  6.                 <category android:name="android.intent.category.LAUNCHER" />  
  7.             </intent-filter>  
  8.         </activity>  
  9.                 <activity android:name=".OtherActivity"  android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                 <action android:name="com.hoo.kay" />  
  12.                 <action android:name="com.hoo.kesen" />  
  13.                 <category android:name="android.intent.category.DEFAULT" />  
  14.                 <category android:name="com.hoo.category.li" />  
  15.                 <data android:scheme="hoo" android:host="www.hoo.com" android:path="/person"/>  
  16.                 <data android:mimeType="image/gif"/>  
  17.             </intent-filter>  
  18.         </activity>  
  19.     </application>  



http://pangaoyuan.iteye.com/blog/1220705

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.