android intent

Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述。Android则根据Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。

Intent属性的设置

1、Action,也就是要执行的动作

常量目标组件动作
ACTION_CALLactivityInitiate a phone call
ACTION_EDITactivitydisplay data for the user to edit
ACTION_MAINactivity

start up as the initial activity of a task,

with no data input and no returned output

ACTION_SYNCactivity

synchronize data on a server with data on 

the mobile device

ACTION_BATTERY_LOW

broadcast

receiver

a warning that the battery is low
ACTION_HEADSET_PLUG

broadcast

receiver

a headset has been plugged into the device ,

or unplugged from it.

ACTION_SCREEN_ON

broadcast

receiver

the screen has benn turned on
ACTION_TIMEZONE_CHANGED

broadcast

receiver

the setting for the time zone has changed

当然,也可以自定义动作(自定义动作在使用时,需要加上包名作为前缀,如“com.example.project.SHOW_COLOR”),并可以定义相应的Activity来处理我们的自定义动作。

2、Data ,也就是执行动作要操作的数据

Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1

对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。

3、type,显示指定Intent的数据类型

一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显示指定类型而不进行推导。

4、category, 被执行动作的附加信息

例如LAUNCHER_CATEGORY表示Intent的接受者应该在Launcher中作为顶级应用出现。

常量意义

CATEGORY_

BROWSABLE

The targe activity can be safely invoked by the browser to display

data referenced by a link -- for example, an image or an e-mail message.

CATEGORY_

GADGET

the activity can be embeded inside of another activity that hosts gadgets.

CATEGORY_

HOME

The activity displays the home screen, the firest screen the user sees

when the device is tured on or when the HOME key is pressed.

CATEGORY_

LAUNCHER

the activity can be the initial activity of a task and is listed in the top-

level application launcher.

CATEGORY_

PREFERENCE

the target activity is a preference panel.

5、component,指定Intent的目标组件的类名称 

通过Android会根据Intent中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。

但是,如果component这个属性有指定的话,将直接使用指定的组件,而不再执行上述查找地过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

6、extras,其它附加信息的集合

使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

Intent的使用方式

1、用Action跳转,隐式Intent

Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent,最终找到匹配的Intent。

(1)如果Intent指明了action,则目标组件的IntentFilter的action列表中就必须包含这个action,否则不能匹配。

(2)如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。

(3)如果Intent中的数据不是content:类型URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme(比如http: mailto:)进行匹配,同上,Intent的scheme必须出现在目标组件的scheme列表中。

(4)如果Intent指定了一个或多个category,这此类别必须全部出现在组件的类别列表中。

2、用类名跳转,显示Intent

Intent intent = new Intent();

intent.setClass(context, targetActivy.class);

// 或者用Intent intent = new Intent(context, targetActivity.class);

startActivity(intent);

最后欢迎大家访问我的个人网站:1024s​​​​​​​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值