android四大组件共性,Android四大组件及意图和意图过滤器
一、Android四大組件
1.Android四大組件
活動? ????Activity
廣播接收者? ?BroadcastReceiver
服務????? Service
內容提供者? ?ContentProvider
2.Android四大組件的共性
1)定義:都是繼承相應的組件的類。
2)清單文件配置:養成一個良好的習慣,創建一個組件類之后,立即在清單文件中的application結
點下配置,并添加name屬性,屬性值為類的帶包全名。
Activity組件????? 配置標簽
BroadcastReceiver? ? 配置標簽
Service????????配置標簽
ContentProvider????配置<>標簽
3.Android四大組件的區別
1)功能不同
2)生命周期不同
二、意圖
1.意圖(Intent)意圖的介紹
我覺得我沒有必要把這個名詞的概念說一遍了,因為官方文檔說得十分地詳細:
An intent is an abstract description of an operation to be performed. ?It
can be used
with
它的意思大概是這樣的:Intent是對將要執行的操作的一種抽象的描述,它可以用來開啟一
個Activity,或者發送它給任何感興趣的廣播接收者BroadcastReceiver組件,還可以與后臺的服務
Service交流。當然,它還可以跨應用交流信息。
意圖的作用:啟動組件并傳遞數據(putExtra與getXxxExtra方法)。
可見,意圖與Android除了ContentProvider組件其它的組件都有關系,至于是什么關系,后面的博
文中我會一一詳述。意圖的分類
意圖分為兩類:顯示意圖和隱式意圖顯示意圖:通過類名來直接創建的意圖叫做顯示意圖,常見的構造形式有如下幾種:
凡是通過.class來構造的Intent都是顯示意圖,常見有2種方式
★ComponentName:組件描述對象
常用構造形式:ComponentName(應用包名, 完整類名)
intent.setComponent(cn);
★直接 new Intent(上下文,類字節碼);
顯示意圖一般用于啟動當前應用中的活動Activity,服務Service,或向當前應用中廣播接收者BroadcastReceiver發送意圖。由于使用類名,目標唯一,所以安全性高。目標不需要在清單里配置意圖過濾器。
隱式意圖:隱式意圖一般用空參構造函數來構造實例,然后通過一些setXxx方法來設置與啟 ? ? ? ? 動目標意圖過濾器相匹配的信息。? 隱式意圖一般用于啟動其他應用中的中的活動Activity,服務Service,或向其它應用中廣播接收者BroadcastReceiver發送意圖。如啟動系統自帶的一些應用。
由于可能匹配不成功或者有多個匹配結果,所以安全性差。目標需要在清單里配置意圖過濾器。
當然,隱式意圖也可以用來啟動本應用中的目標組件,只要它在清單里配置過意圖過濾器。
★★:如果在非Activity(如Service)里利用意圖啟動一個activity,一定要為意圖添加一個
標記intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),要為activity創建一個棧環境
★★:開啟一個activity用startActivity,開啟一個服務用startService。
2.意圖過濾器(IntentFilter)介紹:
看官方文檔:
Structured description of Intent values to be matched. ?An IntentFilter can ? ? ? ?match against actions, categories, and data (either via its type, scheme,
and/or path)
in an Intent.It also includes a "priority" value which is
used to order multiple
matching filters.
IntentFilter objects are often created in XML as part of a package's
它的意思大概是這樣的:對要配置的的意圖的值的描述,它可以通過acion、category、data
來匹配一個意圖。而且意圖過濾器還有一個叫作“優先級”值,用來對多個配置的意圖過濾器進行
排序。
通常意圖過濾器是在清單文件中用intent-filter標簽進行配置的,而需要定
義三個組件結點中。
意圖過濾器的作用:匹配意圖意圖過濾器匹配規則
Filter Rules
A match is based on the following rules. ?Note that
for an IntentFilter to match an Intent, three conditions must hold:
the action and category must match, and
the data (both the data type and data scheme+authority+path if specified) must match.
Actionmatches if any of the given values match the
Intent action; if the filter
specifies no actions, then it will only match
Intents that do not contain an action.
(只要過濾器里至少有1個action與Intent對象匹配即可)
】】action表示動作,就是想要做的事情的名稱。
Categoriesmatch if all of the categories in
the Intent match categories given in the
filter. ?Extra categories in the
filter that are not in the Intent will not cause the
match to fail. ?Note
that unlike the action, an IntentFilter with no categories
will ? ? only match an Intent that does not have any categories.
(過濾器里的所有category必需與Intent里的所有category一一匹配)
】】最常用的取值就2種情況
啟動界面需
要配置
Data Schemematches if any of the given values match the
Intent data's scheme.
The
Intent scheme is determined by calling Note that
scheme matching here is case sensitive, unlike
formal RFC schemes! ?You should thus
always use lower case letters
for your schemes.
(比如排打電話時創建的Intent對象,需要設置setData("tel:" + 電話號碼),這個"tel:"就是data
scheme)
】】最常用的取值package、file、tel
Data Type matches if any of the given values match the
Intent type. ?The Intent
type
is determined by calling
the MIME sub-type, in both the Intent and IntentFilter, so that the
type "audio/*"
will match "audio/mpeg", "audio/aiff", "audio/*", etc. Note that MIME type matching
here is case sensitive, unlike
formal RFC MIME types! ?You should thus always use
lower case letters
for your MIME types.
Intent拓展功能1:android拍照,調用系統相冊,相片上傳
補充:欲知詳情,請查看Android官方文檔。
總結
以上是生活随笔為你收集整理的android四大组件共性,Android四大组件及意图和意图过滤器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TCP/IP 协议栈 -- 编写UDP客
- 下一篇: spring集成 log4j + slf