Intent之Component

1、Intent的概念:
Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的activity, service和broadcast receiver之间的交互。Intent这个英语单词的本意是“目的、意向、意图”。
Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来响应。
二、Intent的相关属性:
Intent由以下各个组成部分:
component(组件):目的组件
action(动作):用来表现意图的行动
category(类别):用来表现动作的类别
data(数据):表示与动作要操纵的数据
type(数据类型):对于data范例的描写
extras(扩展信息):扩展信息
Flags(标志位):期望这个意图的运行模式
三、Component
Intent的Component属性需要接受一个ComponentName对象。
Intent包含如下三个方法。
setClass(Context packageContext,Class

//定义ComponentName
ComponentName componentName=
new ComponentName(EditTextActivity.this,
ShowPictureActivity.class);
//表示登录成功,请跳入另一个界面
Intent intent = new Intent();    
 //intent相当于一个快递员
//设置intent要携带的内容,就是用户名
intent.putExtra("name", userName); 
//第一个参数是关键字key, 第二个参数是携带的字符串数据      
 intent.setComponent(componentName);
 //启动新的界面       
 startActivity(intent);

在另一个Activity中

//获取用户名,从跳转的Intent中获取
Intent intent = getIntent();
//参数是关键字key,与intent.putExtra("name", userName)中第一个参数相同
String userName =
intent.getStringExtra("name");

猜你喜欢

转载自blog.csdn.net/u011164827/article/details/50993470
今日推荐