天天看點

android元件通訊 Intent Filter隐式實作Intent

Intent Filter是android裡非常有特色的一個概念。他的使用者體驗和windows下的檔案管理器的彈出菜單裡的“打開方式”非常相似。在windows中,當使用者選擇了打開方式菜單後,系統讓使用者選擇應用來打開所選擇的檔案。而在android中的檔案已經被uri資源代替了。 

Intent Filter在android中的應用非常普遍,尤其在資源共享中。例如,當使用者選擇了一個圖檔,選擇了共享,我們常常會發現一個選擇清單。這個選擇清單是動态生成的,不是一成不變的。假如你新安裝了facebook應用,那麼facebook就會出現在這個清單裡面。從這個例子可以發現,intent filter的設計使得android系統顯得更加靈活了。 

要實作一個Intent Filter, 我們要在AndroidManifest.xml中加入必要的設定,以通知系統某個activity都能夠處理什麼類型的URI資源,然後要在activity的onCreate中加入必要的代碼以處理系統傳遞過來rui資源。

一個Service、 Activity 或者 Brodcast Reciever 的Intent Filter , 的事件過濾器, 我們用着大家都很了然的說法來描述之吧, 實際上描述了擁有這些過濾器組建在系統中所具有能力。

<code><code>一個Intent 是否能夠通過一個過濾器到達對應的處理器取決于, Intent 所包含的 所有Categroy 是夠都在 過濾器中的catergory 清單裡出現。 換句話說, 沒有 catergory字段的Inteng 可以通過任何過濾器, 而一個過濾器, 必須至少要有一個可以通過的限定,Category 或者 Action。 對于Action 也一樣, 能夠通過Filter 的Itent的 Action </code></code><code><code>必須被列與 Category中。同樣, 沒有Action 的</code></code><code><code>Intent 也可以通過所有的Filter。</code></code>

這裡翻譯文檔中的四個例子情況:

a.如果沒有定義URI 和資料類型, 則,該Intent隻能通過沒有定義URI和 資料類型的 過濾器。

b.隻定義了URI的 Intent 隻能通過隻定義了 URI 的過濾器。

c.隻定義了 資料類型的 Intent 也隻能通過隻定義了 資料類型的過濾器。

d. 如果一個Intent同僚定義了URI和 資料類型, 或者資料類型可以由URI推定, 則隻有具備了對應的資料類型,或者 content: 或者 file: 類型,并且沒有定義URI的過濾器。(這種情況, 你自己代碼一下就明白了, 在寫下去,我也不鳥鳥了。)

如果通向Activities 的Intent 引起了多個響應, 就會出現需要使用者指定的提示, 或者是異常。

差不多就是這樣的情況了, android 的這種機制非常适合移動裝置之間的應用互相調用, 以便在更大的粒度上達成複用。很強大。

/Chapter06_Intent_Filter/src/com/amaker/ch06/app/MainActivity.java

代碼  

package com.amaker.ch06.app;  

import com.amaker.ch06.app.R;  

import android.app.Activity;  

import android.content.Intent;  

import android.net.Uri;  

import android.os.Bundle;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

/**  

 *   

 * Intent Filter 測試  

 */ 

public class MainActivity extends Activity {  

    // 聲明Button  

    private Button btn;  

    private static final String ACTION1 = "com.amaker.ch06.app.TEST_ACTION1";  

    private static final String ACTION2 = "com.amaker.ch06.app.TEST_ACTION2";  

    private static final String ACTION3 = "com.amaker.ch06.app.TEST_ACTION3";  

    private static final String CATEGORY1 = "com.amaker.ch06.app.CATEGORY1";  

    @Override 

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        // 設定内容布局  

        setContentView(R.layout.main);  

        //執行個體化按鈕  

        btn = (Button)findViewById(R.id.Button01);  

        String a = Intent.ACTION_VIEW;  

        // 添加單擊監聽器  

        btn.setOnClickListener(new OnClickListener() {  

            @Override 

            public void onClick(View view) {  

                  Intent intent = new Intent();  

                  //intent.setAction(ACTION1);  

                  //Uri data = Uri.parse("content://com.amaker.ch07.app/abc");  

                  //intent.setData(data);  

                  intent.addCategory(CATEGORY1);  

                  intent.setAction("android.intent.action.VIEW");  

                  intent.setData(Uri.parse("http://www.google.com"));  

                  startActivity(intent);  

            }  

        });  

    }  

/Chapter06_Intent_Filter/src/com/amaker/ch06/app/TestActivity.java

 * 測試Intent Filter  

public class TestActivity extends Activity {  

        setContentView(R.layout.main1);  

/Chapter06_Intent_Filter/res/layout/main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt; 

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    &gt; 

    &lt;Button   

        android:text="測試Intent Filter"   

        android:id="@+id/Button01"   

        android:layout_width="wrap_content"   

        android:layout_height="wrap_content"&gt;&lt;/Button&gt; 

&lt;/LinearLayout&gt; 

/Chapter06_Intent_Filter/res/layout/main1.xml

    &lt;TextView   

        android:id="@+id/TextView01"   

        android:layout_height="wrap_content"&gt;&lt;/TextView&gt; 

/Chapter06_Intent_Filter/AndroidManifest.xml

&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" 

      package="com.amaker.ch06.app" 

      android:versionCode="1" 

      android:versionName="1.0"&gt; 

    &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; 

        &lt;activity android:name=".MainActivity" 

                  android:label="@string/app_name"&gt; 

            &lt;intent-filter&gt; 

                &lt;action android:name="android.intent.action.MAIN" /&gt; 

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt; 

            &lt;/intent-filter&gt; 

        &lt;/activity&gt; 

        &lt;activity android:name="TestActivity" &gt; 

                &lt;action android:name="com.amaker.ch06.app.TEST_ACTION1"/&gt; 

                &lt;action android:name="com.amaker.ch06.app.TEST_ACTION2"/&gt; 

                &lt;action android:name="com.amaker.ch06.app.TEST_ACTION3"/&gt; 

                &lt;action android:name="android.intent.action.VIEW"/&gt; 

                &lt;data android:scheme="content" android:path="com.amaker.ch07.app/abc"/&gt; 

                &lt;data android:scheme="http" android:path="www.google.com" /&gt; 

                &lt;category android:name="android.intent.category.DEFAULT"/&gt; 

                &lt;category android:name="android.intent.category.BROWSABLE" /&gt; 

                &lt;category  android:name="com.amaker.ch07.app.CATEGORY1"/&gt; 

    &lt;/application&gt; 

    &lt;uses-sdk android:minSdkVersion="3" /&gt; 

&lt;/manifest&gt; 

 本文轉自linzheng 51CTO部落格,原文連結:http://blog.51cto.com/linzheng/1080659

繼續閱讀