一、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(上下文,类字节码);

Android四大组件图片 android四大组件的名称_Android四大组件图片

Android四大组件图片 android四大组件的名称_四大组件_02

显示意图一般用于启动当前应用中的活动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四大组件的名称_Android_03