Intent详解

来源:互联网 发布:网页设计排版软件 编辑:程序博客网 时间:2024/05/29 19:08

Intent

Intent是Android组件之间的纽带,有三种基本用法:

1、启动Activity

从一个Activity启动另一个Activity,用方法startActivity()来启动;如果希望第二个Acitvity结束时,能返回一个结果,则使用方法startActivityForResult(),在第一个Activity的方法onActivityResult()中处理返回结果。

2、启动Service

启动Service,可以使用startService()方法。也可以绑定到组件,使用bindService()。

3、发送Broadcast

发送广播的方法有很多,常用的有sendBroadcast(),sendOrderedBroadcast(),sendStickyBroadcast()等。

Intent类型

Intent一般分为显示Intent与隐式Intent

显示Intent:明确指定需要启动或者触发的组件的类名。
隐式Intent:指定需要启动或者触发的组件需要什么条件。

显示Intent是明确指定的,而隐式Intent需要过滤条件,这时候就要用到Intent-Filter。Intent-Filter在AndroidManifest.xml文件中查找所有的intent-filter,通过校对一些属性来进行过滤。那么Intent的属性都有哪些?

Intent包含七大属性

Component、Action、Category、Data、Type、Extra和Flag

其中Component用于明确指定需要启动的目标组件,而Extra则用于携带需要交换的数据。Flag是旗标,主要控制跳转的效果。

Action是完成的一个抽象动作。Category则用于为Action增加额外的附加信息。(setAction()、addCategory())
一个Intent对象只能有一个Action属性,但可以有多个Category属性。
如果要自定义Action,建议将包名作为前缀。
下面是启动Activity的category

<category android:name="android.intent.category.LAUNCHER" />

Intent主要通过Action、Data/Type、Category来进行过滤。
(注:Data与Type会互相覆盖,后设置的生效)
Data属性通常用于向Action属性提供操作的数据。Data属性接受一个Uri对象。

Uri格式:
scheme://host:port/path

Type属性用于指定该Data所指定Uri对应的MIME类型,MIME可以是自定义的。

在intent-filter中至少包含有action这个属性,如果没有,不会有任何的Intent可以与之匹配。

对于Android系统的以下服务,都有相应的Intent启动。

1、Alarm Clock
2、Calender
3、Camera
4、Contacts/People APP
5、Email
6、File Storage
7、Maps
8、Music or Video
9、Phone
10、Settings
11、Text Messaging
12、Web Browser

0 0
原创粉丝点击