转载:Android Intent Filter-判断intent传递对象
原文地址:http://www.apkbus.com/android-1386-1-1.html
一.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,datacategory三。也就是filter只intent的三料做比作,而在每filter可以同存在著多data actioncategory
然每Intent Filter都於IntentFilter,但因Android系在元件就必需其相容性,所以Intent Filter都是以XML方式在AndroidManifest.xml,而不以Java Code去生。唯一的例外是broadcast receivers的intent filter可以透Context.registerReceiver()定
例
JavaCode定intent Object的Category
六.Intent Test-Category Test
1.若intent有定Category,必intent object可以通所有filter定 的Category,Test才pass2.若intent有定Category,定pass3.行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都包含了URIdata type (MIME media type)
而URI以scheme, host, port, path表示
也就是可以把一完整的URI看成
scheme://host:port/path
若host指定,port可略
data type可以用*代表允任何型的料
如text/*" or "audio/*"
例
URI
data在filterintentdata比的原是1.filter有定任何data tag,而intent也有定URIdata type, 那test就通2.filter有定URI,有定data type,而intent object也是只有URI有 data type,候通,通常只生在mailto: tel: 3.filter只有定data type 有定URI,而intent object有定相同 的data typeURI通4.filter定的data typeURI都intent Object定的即通 若filter只有定data type而URI不定,只有在intent Object的 data type有,而URIcontent: 或file:才通
八.Example
1.定元件可以示local端 任何Image案
要由local contentProvider示案,即可,不需再指定URI部份用content:或file:
2.定允播放自internet的任何影片
使用者透web page要一源,先使用html page看看,若不行,未指定象
的intent一,去查找哪一Activity合去源,若不知道,Download Manager去下
3.定行的Activity
不需透任何intent去,在App即自行Activity
九.IntentIntent 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清