Android之Intent
1.题记
Intent提供了一种通用的消息系统,它允许在你的应用程序与其他的应用程序间传递Intent来执行动作和产生事件。通过使用Intent可以**Android应用的三个核心组件:活动、服务、广播接收器。
2.Intent的划分与匹配
Intent可以划分成显示意图和隐式意图。
显示意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显示意图,显示意图明确指定了Intent应该传递给哪个组件。
隐式意图:没有明确指定组件名的Intent为隐式意图。Android系统会根据隐式意图中设置的动作、类别、数据找到最合适的组件来处理这个意图。隐式意图定义如下:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <data android:mimeType="vnd.android.cursor.item/phone" /> </intent-filter>
关于隐式意图Android系统会根据Intent Filter(意图过滤器)来匹配隐式Intent。下面是讲解如何进行意图匹配的。
动作匹配:
一个意图对象只能指定一个动作名称,而一个过滤器可能列举多个动作名称。如果意图对象或过滤器没有指定任何动作,结果将如下:
- 如果过滤器没有指定任何动作,那么将阻塞所有的意图,因此所有的意图都会测试失败。没有意图能够通过这个过滤器。
- 另一方面,只要过滤器包含只是一个动作,一个没有指定动作的意图对象自动通过这个测试。
类别匹配:
对于一个能够通过类别匹配测试的意图,意图对象中的类别必须匹配过滤器中的类别。这个过滤器可以列举其他的类别,但它不能遗漏在这个意图中的任何类别。
数据匹配:
当一个意图对象中的URL被用类和一个过滤器中的URI比较时,比较的是URI的各个组成部分。
3.Intent数据传输
下面通过在不同Activity之间的数据传输的例子,讲述Intent的两种不通数据传输方式。
3.1Intent putExtra 方法
Intent的putExtra方法几乎包括了所有的基本类型,如下图:
第一个参数 name:表示携带的数据的名称
第二个参数 value:表示数据的值。
3.2Bundle传递数据
Bundle类似map对象,Bundle提供了常用类型的装填数据方法和获取方法 ,如图:
具体实例如下源码所示:
package com.sulang.android.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button nextButton =(Button) findViewById(R.id.next);
nextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//**OtherActivity
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", "Wilian");
bundle.putInt("age", 22);
//附带上额外的数据
intent.putExtras(bundle);
//如果需要打开的Activity向前面Activity返回数据,就必须使用此方法打开Activity
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//用来提示显示返回的信息
if(requestCode==1)
{
String helloStr=data.getStringExtra("hello");
TextView hello =(TextView) this.findViewById(R.id.hello);
hello.setText(helloStr);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
package com.sulang.android.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/*
*@author 七里香的悔恨,2011-3-17
*OtherActivity.java
*Blog:[url]http://bigboy.iteye.com/[/url]
*/
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
//得到**该组件的意图
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
final String name=bundle.getString("name");
int age=bundle.getInt("age");
TextView view = (TextView)this.findViewById(R.id.show);
view.setText("名称:"+name+" 年龄:"+age);
Button backButton = (Button)this.findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent data=new Intent();
data.putExtra("hello", "hello "+name);
//设置返回码和数据,返回码可以任意
OtherActivity.this.setResult(1, data);
//关闭Activity
OtherActivity.this.finish();
}});
}
}
至此Intent已简单介绍完毕。
源代码如下: