`

【转】Android 重要组件 之 Intent(一)

阅读更多

Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents.

 

一、Intent

1、Intent属性、解析
Intent是对执行某个操作的一个抽象描述,其描述的内容包括,对执行动作Action的描述、对操作数据的描述、还有4种附加属性的描述。
Action、data、catagory、extra、component、type
当指定了component属性后,就是显式的指定了目标组件,也就是接收端。如果没有明确指定目标组件,那么Android系统会使用 Intent 里的(action,data,category)三个属性来寻找和匹配接收端。

 

2、IntentFilter
应用程序组件可以使用IntentFilter来向系统说明自己可以响应和处理那些Intent请求。

 

二、Service
1、Service是运行在后台的程序。启动的方法有两种:
startService()、bindService()

2、Service lifecycle
继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法。

AIDL... 这个以后在看!

 

三、BroadcastReceiver
Broadcast Receiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。
实现BroadcastReceiver:
1、继承BroadcastReceiver类
2、重写onReceive方法
3、AndroidManifest.xml中注册此Receiver,监听action

调用sendBroadcast(intent) 广播出去。

 

四、Content Provider
1、Content Provider提供了程序之间共享数据的方法,一个程序可以使用Content Provider 定义一个URI,
提供统一的操作接口,其他程序可以通过此URI访问指定的数据,进行数据的增、删、改、查。

2、ContentResolver
ContentResolver接口可以访问ContentProvider提供的数据。

3、ContentProvider和ContentResolver当中用到了Uri的形式通常有两种,一种是指定全部数据,另一种是指定某个ID的数据。我们看下面的例子。
content://contacts/people/  这个Uri指定的就是全部的联系人数据。
content://contacts/people/1 这个Uri指定的是ID为1的联系人的数据。

 

Content Provider的使用,直接参看SDK自带的NotePad实例。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics