Intent
Class Overview 类的概述
Intent Structure Intent 构成
Intent / Activity / Service / BroadReceiver 之间的关系:
Intent的分类
Intent的用法:
extends Object
implements Parcelable Cloneable
java.lang.Object
android.content.Intent
Class Overview 类的概述
An intent is an abstract description of an operation to be performed. It can be used with
startActivity
to launch an Activity
, broadcastIntent
to send it to any interested BroadcastReceiver
components, and startService(Intent)
or bindService(Intent, ServiceConnection, int)
to communicate with a background Service
.
Intent是对要执行的操作的抽象的描述.可以使用startActivity()方法启动一个Activity,broadcastIntent可以发送任何由BroadcastReceiver接收的消息.可以使用startService(Intent)或者bindService(Intent,ServiceConnection,int)与后台运行的Service进行通信.
Intent Structure Intent 构成
- action The general action to be performed, such as
ACTION_VIEW
,ACTION_EDIT
,ACTION_MAIN
, etc. 一般是要执行的动作 - data The data to operate on, such as a person record in the contacts database, expressed as a
Uri
.对于数据的操作.Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。 - type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
- category(类别),被执行动作的附加信息。例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。
Intent / Activity / Service / BroadReceiver 之间的关系:

Intent的分类
- 显示Intent 指定具体的目标组件处理(即在构造Intent对象时就指定接收者,下面制定了AnotherActivity为接受者)
–startActivity(new Intent(ActivityLifecycle.this, AnotherActivity.class));- 隐式Intent 由系统接收并决定如何处理 (即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,有利于降低发送者和接收者之间的耦合.)。startActivity(new Intent(Intent.ACTION_DIAL));
Intent filter
< activityandroid:name = ".ZYCActivity"android:label = "@string/app_name" >< intent-filter >< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" /></ intent-filter ></ activity >
在filter中定义相关的匹配规则,告知系统对应的component可以接受哪些intent.

比如:打电话的时候action是dial,data是134XXXXXXXX;百度查询的时候action是View,data是http://www.baidu.com;
Intent的用法:
- 无参跳转
Intent mIntent = new Intent(当前Activity.this,要跳转的activity.class);startActivity(mIntent);
- 向下一个Activity传递数据(使用Bundle和Intent.putExtras)
跳转并传递数据Intent mIntent = new Intent(当前Activity.this,要跳转的activity.class);Bundle bundle = new Bundle();bundle.putString("name","zhaoyongchun");mIntent.putExtra(bundle);startActivity(mIntent);接收数据Bundle bundle = getIntent().getExtra();String name = bundle.getSring("name");
- 向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)
Intent mIntent= getIntent();Bundle bundle = new Bundle();bundle.putString("name","zhaoyongchun");mIntent.putExtra(bundle);setResult(RESULT_OK,mIntent);
- 回调上一个Activity的结果处理函数(onActivityResult)
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CODE){
if(resultCode==RESULT_CANCELED)
setTitle("cancle");
else if (resultCode==RESULT_OK) {
String temp=null;
Bundle bundle=data.getExtras();
if(bundle!=null) temp=bundle.getString("name");
setTitle(temp);
}
}
}
Intent 实例
1. Uri uri = Uri.parse("http://google.com");
- 显示网页
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. startActivity(it);
1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
- 显示地图
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. startActivity(it);
4. //其他 geo URI 範例
5. //geo:latitude,longitude
6. //geo:latitude,longitude?z=zoom
7. //geo:0,0?q=my+street+address
8. //geo:0,0?q=business+near+city
9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
- 路径规划
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. startActivity(it);
4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
1. //叫出拨号程序
- 打电话
2. Uri uri = Uri.parse("tel:0800000123");
3. Intent it = new Intent(Intent.ACTION_DIAL, uri);
4. startActivity(it);
1. //直接打电话出去
2. Uri uri = Uri.parse("tel:0800000123");
3. Intent it = new Intent(Intent.ACTION_CALL, uri);
4. startActivity(it);
5. //用這個,要在 AndroidManifest.xml 中,加上
6. //<uses-permission id="android.permission.CALL_PHONE" />
1. //调用短信程序
- 传送SMS/MMS
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. it.putExtra("sms_body", "The SMS text");
4. it.setType("vnd.android-dir/mms-sms");
5. startActivity(it);
1. //传送消息
2. Uri uri = Uri.parse("smsto://0800000123");
3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
4. it.putExtra("sms_body", "The SMS text");
5. startActivity(it);
1. //传送 MMS
2. Uri uri = Uri.parse("content://media/external/images/media/23");
3. Intent it = new Intent(Intent.ACTION_SEND);
4. it.putExtra("sms_body", "some text");
5. it.putExtra(Intent.EXTRA_STREAM, uri);
6. it.setType("image/png");
7. startActivity(it);
1. Uri uri = Uri.parse("mailto:xxx@abc.com");
- 传送 Email
2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
3. startActivity(it);
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");
4. it.setType("text/plain");
5. startActivity(Intent.createChooser(it, "Choose Email Client"));
1. Intent it=new Intent(Intent.ACTION_SEND);
2. String[] tos={"me@abc.com"};
3. String[] ccs={"you@abc.com"};
4. it.putExtra(Intent.EXTRA_EMAIL, tos);
5. it.putExtra(Intent.EXTRA_CC, ccs);
6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");
7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
8. it.setType("message/rfc822");
9. startActivity(Intent.createChooser(it, "Choose Email Client"));
1. //传送附件
2. Intent it = new Intent(Intent.ACTION_SEND);
3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
5. sendIntent.setType("audio/mp3");
6. startActivity(Intent.createChooser(it, "Choose Email Client"));
Uri uri = Uri.parse("file:///sdcard/song.mp3");
- 播放多媒体
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setType("audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);