Intent 是对要执行的操作的抽象描述,通常包含一些数据。
作用:通过它来在Activity 、Services、和broadcast receivers之间通信交互。
Intent包含几个可选的信息:
--------------------------------------------------------
摘自安卓源代码:Intent 类的成员
private String mAction;
private Uri mData;
private String mType;
private ComponentName mComponent;
private HashSet<String> mCategories;
private Bundle mExtras;
private String mPackage;
------------------------------------------------
Component name
Action: 字符串,要执行的动作
Category: 字符串,一般用来指示Action的额外信息
Extras: Bundle类型的数据,用于传递数据
Data: Uri类型的数据
Type: Data对应的MINI类型
详解:
有两种类型的Intent
1显式的:直接指明要调用的组件名(Component name)
2隐式的:未指明要调用的组件名,而是根据Intent的信息,找到最匹配的目标
隐式Intent 如何寻找目标组件的:通过组件的 intent filter 域信息进行匹配。
组件通常在AndroidManifest.xml文件中注册自己感兴趣的Intent Filter。
Xml文件中,intent filter只通过可选的三个域 Action Data Category来注册
一个组件可注册几个intent filter,只要其中有一个intent符合,则匹配通过。
对某一个intent而言,只有三个域 Action Data Category全部匹配,这个intent才匹配通过
例子1 显式调用,不需要Intent Filter
ComponentName myNme=new ComponentName(MyActivity.this,OtherActiviy.class);
Intent myIntent=new Intent();
myIntent.setComponent(myNme);
StarActivity(myIntent);
更简洁的写法
Intent myIntent=new Intent(MyActivity.this,OtherActiviy.class);
StarActivity(myIntent);
例子2 用 Bundle 在Activity之间传数据见(疯狂Android讲义P181)
例子3 见Android SDK自带例子NotesList