Intent我的理解就是在安卓的各个组件之间传递消息和数据的,这个各个组件可以是activity与activity之间,也可以是service,Broadcast,可以是同一个应用程序间传递,也可以是跨应用之间传递:
组件名称 | 方法名称 |
Activity | startActvity( ) startActivity( ) |
Service | startService( ) bindService( ) |
Broadcasts | sendBroadcasts( ) sendOrderedBroadcasts( ) sendStickyBroadcasts( ) |
几种属性:
1.Action是指Intent要执行的动作,是一个字符串常量。SDK中定义了一些标准的Action常量如下表所示:
Constant | Target component | Action |
ACTION_CALL | activity | Initiate a phone call. |
ACTION_EDIT | activity | Display data for the user to edit. |
ACTION_MAIN | activity | Start up as the initial activity of a task, with no data input and no returned output. |
ACTION_SYNC | activity | 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 been turned on. |
ACTION_TIMEZONE_CHANGED | broadcast receiver | The setting for the time zone has changed. |
2.Intent的Data属性
Intent的Data属性是执行动作的URI和MIME类型,不同的Action有不同的Data数据指定。比如:ACTION_EDIT Action应该和要编辑的文档URI Data匹配,ACTION_VIEW应用应该和要显示的URI匹配。
public void invokeWebBrowser(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com.hk"));
startActivity(intent);
}
3.Intent的Category属性
<category
android:name="android.intent.category.LAUNCHER" /> 代表该目标Activity是该应用所在task中的初始Activity并且出现在系统launcher的应用列表中。
Intent中的Category属性是一个执行动作Action的附加信息。比:CATEGORY_HOME则表示放回到Home界面,ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个。下表是SDK文档中关于Category的
信息。
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);// 添加Action属性
intent.addCategory(Intent.CATEGORY_HOME);// 添加Category属性
startActivity(intent);// 启动Activity
}
4.Intent的Type属性
Intent的Type属性显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
5.Intent的Compent属性
Intent的Compent属性指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。
6.Intent的Extra属性
Intent的Extra属性是添加一些组件的附加信息。比如,如果我们要通过一个Activity来发送一个Email,就可以通过Extra属性来添加subject和body。下面的例子在第一个Activity的EditText输入用户名,该年龄保存在Intent的Extras属性中。当单击Button时,会在第二个Activity中显示用户名。
public void onClick(View v) {
Intent intent = new Intent();
//设置Intent的class属性,跳转到SecondActivity
intent.setClass(FirstActivity.this, SecondActivity.class);
//为intent添加额外的信息
intent.putExtra("useName", etx.getText().toString());
//启动Activity
startActivity(intent);
Bundle对象:
Bundle和Intent有着密不可分的关系,主要负责为Intent保存附加参数信息,它实现**droid.os.Paracelable接口,内部维护一个Map类型的属性,用于以键值对的形式存放附加参数信息。在我们使用Intent的putExtra方法放置附加信息时,该方法会检查默认的Bundle实例为不为空,如果为空,则新创建一个Bundle实例,然后将具体的参数信息放置到Bundle实例中。我们也可以自己创建Bundle对象,然后为Intent指定这个Bundle即可,如下:
public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
Bundle bundle = new Bundle();
bundle.putInt("id", 0);
bundle.putString("name", "scott");
intent.putExtras(bundle);
startActivity(intent);
}
需要注意的是,在使用putExtras方法设置Bundle对象之后,系统进行的不是引用操作,而是复制操作,所以如果设置完之后再更改bundle实例中的数据,将不会影响Intent内部的附加信息。那我们如何获取设置在Intent中的附加信息呢?与之对应的是,我们要从Intent中获取到Bundle实例,然后再从中取出对应的键值信息:
Bundle bundle = intent.getExtras();
int id = bundle.getInt("id");
String name = bundle.getString("name");
我觉得和把数据直接放在extra里比的话,就是直接放用的时候就是直接引用,而使用bundle就是复制使用,不会改变原来intent中引用的数据,所以bundle还是有存在的必要