http://www.moandroid.com/?p=1651

应用程序的组件为了告诉Android自己能响应、处理哪些隐式Intent请求,可以声明一个甚至多个Intent Filter。每个Intent Filter描述该组件所能响应Intent请求的能力——组件希望接收什么类型的请求行为,什么类型的请求数据。比如之前请求网页浏览器这个例子中,网 页浏览器程序的Intent Filter就应该声明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及与之相关的请求数据是网页地址URI格式。如何为组件声明自己的Intent Filter? 常见的方法是在AndroidManifest.xml文件中用属性<Intent-Filter>描述组件的Intent Filter。

前面我们提到,隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动作、数据以及类别。实际上,一个隐式Intent请求要能够传递给目标组件,必要通过这三个方面的检查。如果任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。接下来我们讲解这三方面检查的具体规则。

 

1.动作测试

<intent-filter>元素中可以包括子元素<action>,比如:
<intent-filter>
<action android:name=”com.example.project.SHOW_CURRENT” />
<action android:name=”com.example.project.SHOW_RECENT” />
<action android:name=”com.example.project.SHOW_PENDING” />
</intent-filter>
一条<intent-filter>元素至少应该包含一个<action>,否则任何Intent请求都不能和 该<intent-filter>匹配。如果Intent请求的Action和<intent-filter>中个某一 条<action>匹配,那么该Intent就通过了这条<intent-filter>的动作测试。如果Intent请求 或<intent-filter>中没有说明具体的Action类型,那么会出现下面两种情况。
(1) 如果<intent-filter>中没有包含任何Action类型,那么无论什么Intent请求都无法和这条<intent-filter>匹配;
(2) 反之,如果Intent请求中没有设定Action类型,那么只要<intent-filter>中包含有Action类型,这个Intent请求就将顺利地通过<intent-filter>的行为测试。

2.类别测试

<intent-filter>元素可以包含<category>子元素,比如:
<intent-filter . . . >
<category android:name=”android.Intent.Category.DEFAULT” />
<category android:name=”android.Intent.Category.BROWSABLE” />
</intent-filter>
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过测试,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。

3.数据测试

数据在<intent-filter>中的描述如下:
<intent-filter . . . >
<data android:type=”video/mpeg” android:scheme=”http” . . . />
<data android:type=”audio/mpeg” android:scheme=”http” . . . />
</intent-filter>
<data>元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。其中,用setData()设定的Inteat请求的URI数据类型和scheme必须与IntentFilter中所指 定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。

4.简单例子说明

讲解完Intent基本概念之后,接下来我们就使用Intent激活Android自带的电话拨号程序,通过这个实例你会发现,使用Intent并不像其概念描述得那样难。最终创建Intent的代码如下所示。
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(“tel://13800138000″));
创建好Intent之后,你就可以通过它告诉Android希望启动新的Activity了。
startActivity(i);
Activity启动后显示界面如下:

5.总结说明

这篇文章是我刚开始学习Android时看到的,当时理解的不是很深入,现在再回头看这篇文章总结的很详细,在这里与大家分享。

 

 

 

 

http://www.cnblogs.com/hdjjun/archive/2010/06/02/1749864.html

Android的Intent和IntentFilter应用说明一例

很多人对文档中的Intent和IntentFilter不理解是什么意思,我这里举例解释下。
Intent字面意思就是目标,目的。通俗一点, 需要达成某些目标,则需要提供一些动作,这些目标的分类,以及达成这些目标所需要的一些数据等等。Android中的Intent通过 Action,Category和data等属性进行了相应的描述,我们想做某些事情(达成某些目标),就需要填写这些参数的部分或全部,这样 Android才会帮助我们自动的去进行某些操作。
IntentFilter是配合Intent而生的,你有目标行动或者结果,那么那些行动和结果就会有他完成的特定要求,这些要求就是IntentFilter,可以理解为Intent和IntentFilter是相对应的。

<activity android:name=".TestService" android:label="@string/app_name">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
                
<category android:name="android.intent.category.LAUNCHER" />
                
<action android:name="android.intent.action.STORE_REQUEST"></action>
                
<category android:name="android.intent.category.ALTERNATIVE" />
                
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
            
</intent-filter>

            
<intent-filter>
                
<action android:name="android.intent.action.TIGERTIAN"></action>
                
<category android:name="android.intent.category.DEFAULT"></category>
                
<data android:scheme="x-id"></data>
            
</intent-filter>
            
            
<intent-filter>
                
<action android:name="android.intent.action.EDIT"></action>
                
<category android:name="android.intent.category.DEFAULT"></category>
            
<category android:name="android.intent.category.BROWSABLE"></category>
</intent-filter>
        
</activity>

上面的Activity有三个Filter,第一个是给Android系统用的,表示这个Activity可以显示在桌面上(Launcher中)。同时Alternative表明,这个Activity可以
变成OptionMenu,供其他Activity直接调用。
后面两个Filter就是我自定义的了,第二个Filter可以在其他Activity中用如下方法直接调用:

                Uri uri = Uri.parse("x-id://www.google.com/getDetails?id=123");
                Intent in 
= new Intent();
                in.setAction(
"android.intent.action.TIGERTIAN");
                in.addCategory(Intent.CATEGORY_DEFAULT);
                in.setData(uri);
                
//in.setClassName("com.tigertian.service", "com.tigertian.service.TestService");
                TestActivity.this.startActivity(in);

在 Filter配置中CATEGORY_DEFAULT是不可缺少的,想调用这个Service,可以不指定Class,但其他条件必须匹配 (CATEGORY_DEFAULT可以不设置,Android默认会自动加上),通过Action,category和data就可以调用相应的 Activity了,这是Android帮你做的,当然如果系统中存在多个匹配这些条件的Activity或者Service,Android根据优先级 进行调用。
第三个调用方式如下:

                Uri uri = Uri.parse("x-id://www.google.com/getDetails?id=123");
                Intent in 
= new Intent();
                in.setAction(Intent.ACTION_EDIT);
                in.addCategory(Intent.CATEGORY_BROWSABLE);
                
//in.setData(uri);
                
//in.setComponent(new ComponentName("com.tigertian.service", "com.tigertian.service.TestService"));
                TestActivity.this.startActivity(in);

看到没有?可以不指定CATEGORY_DEFAULT,Android自动帮你添加。就是这么简单。 

 

TrackBack:http://www.blogjava.net/TiGERTiAN/archive/2010/02/03/311827.html

 

 

http://ozzysun.blogspot.com/2010/10/android-intent-filter-intent.html

Android Intent Filter-判斷intent傳遞對象

一.Intent接收原理
當使用者發送一個intent出來,要求元件去執行動作,如果這個intent裡有很清楚的設定了ComponentName,那麼intent就會直接 被送到指定的元件,並啟動該元件,如果沒有設定,則會由Android系統自動去判斷該把這intent送到哪個元件上啟動他。

大多數在本身專案內元件可以處理的動作,intent通常都會直接指明要給哪個元件處理,如果沒有指定元件名稱的intent大多是用來啟動其他Application上的元件

二.Intent Filter的作用
Android系統如何判斷哪個元件可以接收哪個intent,就是依靠在ManiFest檔案內,宣告元件(Activity,Service)時所加 入的Intent Filter設定,每個Activity內可以設定0~多組的intent filter,每一組的Intent Filter都是一份比對規則

當intent發出來時,系統會去檢查Manifest內各元件內的intent filter,而啟動適合的元件,若元件沒有設定filter,那就只能接收到有清楚指定component的intent

三.Intent Filter
Intent filter內會設定的資料包括action,data與category三種。也就是說filter只會與intent裡的這三種資料做比對動作,而在每個filter內可以同時存在著多個data action與category

雖然每個Intent Filter都屬於IntentFilter類別,但因Android系統在元件啟動錢就必需測試其相容性,所以Intent Filter都是以XML方式寫在AndroidManifest.xml檔內,而不以Java Code去產生。唯一的例外是broadcast receivers的intent filter可以透過Context.registerReceiver()來動態設定

Context.registerReceiver(BroadcastReceiver receiver, IntentFilter filter)

在註冊時,把filter動態加入

Intent Filter內要設定的值,包括action,data與category都定義在Intent Class內,也就是說在Intent物件內設定這3種資料的值與Intent Filter內要設定這3種資料的值都定義在Intent Class內,在JavaCode裡,可以透過Class引用參數,但在Manifest內設定,需要設定字串值,就要去查Class內參數實體的字串值

例:Manifest.xml設定filter

 <intent-filter . . . >  
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
. . .
</intent-filter>


JavaCode設定intent Object的Category

intent.setCategory(Intent.CATEGORY_DEFAULT);


四.Intent Test 找到要接收intent的元件
當intent發出來時,會與元件在Manifest內所定義的intent filter做比對,比對時需進行3個test,當這3個test都通過時,才會確認這元件可以接手處理這Intent。若同時有不只一個的 Activity或Service通過Test,那系統會問你要啟動哪一個,若都不符合,則會發生錯誤

五.Intent Test-Action Test

1.當一個intent object若沒有指定Action,則這項pass
2.filter內至少要有一項action,若都沒有就沒有任何intent
可以通過這filter


 <intent-filter . . . >  
<action android:name="com.example.project.SHOW_CURRENT" />
<action android:name="com.example.project.SHOW_RECENT" />
. . .
</intent-filter>


六.Intent Test-Category Test

1.若intent有設定Category,必須intent object可以通過所有filter設定
的Category,Test才會pass
2.若intent沒有設定Category,則設定pass
3.當執行startActivity()而使用沒有明確設定目標的intent,都會當作他
至少會有一個Category: Intent.CATEGORY_DEFAULT
(也就是"android.intent.category.DEFAULT")


因此若Activity要能夠接收到這intent,則在Mainfest內定義這Activity的intent filter部份需要有Category為"android.intent.category.DEFAULT"的值,才會有可能接收到,也就是說在 Manifest內的Activity,至少要有Intent.CATEGORY_DEFAUL(也就 是"android.intent.category.DEFAULT")的值,而在已經設定為啟動的Activity內多設定 Intent.CATEGORY_DEFAUL,也可以但並無用處

在filter內設定category值,需設定完整字串值,這部份查Intent Class,因為在xml tag接受的值是字串,而在Java Code內設定intent的Category就可以用Class的常數去書寫

七.Intent Test-Data Test
每個data tag都包含了URI與data type (MIME media type)
而URI會以scheme, host, port,與 path來表示
也就是可以把一個完整的URI看成

scheme://host:port/path

若host沒指定,則port可略
data type可以用*代表允許任何型態的資料
如text/*" or "audio/*"

URI為

content://com.example.project:200/folder/subfolder/etc


則設定Filter的寫法為 

 <intent-filter . . . >  
<data android:mimeType="video/mpeg" android:scheme="content" android:host="com.example.project" android:port="200" android:path="folder/subfolder/etc"/>
<data android:mimeType="audio/mpeg" android:scheme="http" . . . />
. . .
</intent-filter>


data在filter與intent內data比對的原則是

1.當filter內沒有設定任何data tag,而intent內也沒有設定URI與data type,
那這test就通過
2.當filter有設定URI,沒有設定data type,而intent object也是只有URI沒有
data type,這時候通過,這通常只會發生在mailto: 與tel: 時
3.當filter內只有設定data type 沒有設定URI,而intent object有設定相同
的data type無URI則通過
4.當filter內設定的data type與URI都與intent Object內設定的對應即通過
若filter內只有設定data type而URI不設定,只有在intent Object內的
data type有對應,而URI為content: 或file:才會通過


八.Example
1.設定元件可以顯示local端 任何Image檔案

 <data android:mimeType="image/*" />  

當要由local contentProvider顯示檔案,這樣寫即可,不需再指定URI部份用content:或file:
2.設定允許播放來自internet的任何影片檔 

 <data android:scheme="http" android:type="video/*" />  

當使用者透過web page連結要開啟一個資源,會先使用html page開啟看看,若不行,則會與未指定對象
的intent一樣,去查找哪一個Activity適合去開啟這個資源,若不知道,則會開啟Download Manager去下載
3.設定啟動預設執行的Activity

 <intent-filter . . . >  
<action android:name="code android.intent.action.MAIN" />
<category android:name="code android.intent.category.LAUNCHER" />
</intent-filter>

不需透過任何intent去觸發,在開啟App即會自動執行這個Activity

九.Intent與Intent Filter
Intent Filter的作用,不僅是找出intent所要啟動的元件,還包括了設定元件該出現在Device的相關訊息
例如在Filter內設定了
"android.intent.action.MAIN"與"android.intent.category.LAUNCHER"
這元件會出現在device最上層的Launch清單上

若元件的filter設定了android.intent.category.HOME"
那這元件就會顯示在Home screen上

利用PackageManager Class的相關method針對指定intent找出適合的元件
透過query..() method可以讓你找到可以接收指定intent的component
透過resolve..()可以找到最適合接收指定intent的component
例如queryIntentActivities()可以取得所有適合的Activity清單

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()