Intent
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。在SDK中给出了Intent作用的表现形式为:
· 通过Context.startActivity() orActivity.startActivityForResult()启动一个Activity;
· 通过Context.startService()启动一个服务,或者通过Context.bindService()和后台服务交互;
· 通过广播方法(比如Context.sendBroadcast(),Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()) 发给broadcast receivers。
Intent属性的设置,包括以下几点:(以下为XML中定义,当然也可以通过Intent类的方法来获取和设置)
(1)Action,也就是要执行的动作
SDk中定义了一些标准的动作,包括
Constant | Target component | Action |
ACTION_CALL | activity | 打开一个电话拨号程序,并立即使用Intent的数据URI所提供的号码拨打一个电话。一般情况下,使用ACTION_DIAL更好 |
ACTION_CALL_BUTTON | activity | 当用户按下硬件的“拨打按钮”时触发,通过会调用拨号的Activity |
ACTION_ALL_APPS | activiity | 打开一个列出所有已安装应用程序的Activity,通常,此操作由启动器处理 |
ACION_ANSWER | activity | 打开一个处理来电的Activity,通常这个动作时由本地电话拨号程序进行处理 |
ACTION_BUG_REPORT |
| 显示一个可以报告bug的Activity,通常由本地报告机制处理 |
ACTION_DELETE | activity | 启动一个Activity,允许删除Intent的数据URI中指定的数据 |
ACTION_DIAL | activity | 打开一个拨号程序,要拨打的号码由Intent的数据URI预先提供,默认情况下,这是由本地Android电话拨号程序进行处理的。拨号程序可以规范大部分号码样式,如tel:555-1234、tel:(212)555 1212;都是有效号码 |
ACTION_EDIT | activity | 请求一个Activity,要求改Activity可以编辑Intent的URI中的数据 |
ACTION_INSERT |
| 打开一个能够在Intent的数据URI指定的游标处插入新的Activity,当作为子Activity调用的时候,它应该返回一个指向新插入项的URI |
ACTION_PICK | activity | 启动一个子Activity,它可以让你从Intent的数据URI指定的Content Provider中选择一个项,当关闭时,它应该返回所选择的项的URI,启动Activity与选择的数据有关,例如,传递content://contacts/people将会调用本地联系人列表 |
ACTION_SEARCH | activity | 通常用于启动特定的搜索Activity,如果没有在特定的Activity上触发它,就会提示用户从所有支持搜索的应用程序中做出选择 |
ACTION_SEARCH_LONG_PRESS |
| 允许截获对硬件搜索键的长按操作。通常由系统处理,以提供语音搜索的快捷方式 |
ACTION_SENDTO | activity | 启动一个Activity来向Intent的数据URI所指定的联系人发送一条消息 |
ACTION_SEND | activity | 启动一个Activity,该Activity会发送Intent中指定的数据。接收人需要由解析的Activity来选择。使用setType可以设置要传输的数据的MIME类型。数据本身应该根据它的类型,使用EXTEA_TEXT或者EXTRA_STREAM存储为extra。对于E-mail,本地Android 的应用程序也可以使用EXTRA_EMAIL、EXTRA_CC、EXTRA_BCC和EXTRA_SUBJECTj键来接收extra。应该只使用ACTION_SEND动作向远程接收人发送数据(而不是设备上的另外一个应用程序) |
ACTON_VIEW | activity | 最常见的通用动作。视图要求以最合理的方式查看Intent的数据URI中提供的数据。不同的应用程序将会根据所提供的数据的URI模式来处理视图请求。一般情况下,http:地址会打开浏览器,tel:地址会打开拨号程序以拨打该号码,geo:地址会在Google地图应用程序中显示出来,而联系人信息将会在联系人管理器中显示出来 |
ACTION_WEB_SEARCH | 浏览器 | 打开一个浏览器,根据searchManager.QUERY键提供的查询执行web搜索 |
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. |
当然,也可以自定义动作(自定义的动作在使用时,需要加上包名作为前缀,如"com.example.project.SHOW_COLOR”),并可定义相应的Activity来处理我们的自定义动作。
(2)Data,也就是执行动作要操作的数据,允许指定组件可以来执行的数据类型;根据情况,也可以包含多个数据标签,可以使用以下属性的任意组合来指定你的组件所支持的数据:
1) Android:host 指定一个有效的主机名(如google.com)
2) Android:minetype指定组件可以执行的数据类型,如<type android:value=”vnd.android.cursor.dir/*”/>将匹配所有的Android cursor。
3) Android:path 指定URI的有效路径值,如/transport/boats/
4) Android:port 指定主机的有效端口
5) Android:scheme 要求一种该特定的模式,如content 或http
Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。
(3)type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
(4)category(类别),被执行动作的附加信息。例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。还有其他的为
Constant | Meaning |
CATEGORY_BROWSABLE | 指定一个在浏览器内部可用的动作。当一个Intent在浏览器内部触发时,它总是会包含BROWSABLE类别,如果想让应用程序响应浏览器内触发的动作(如,截获指向特定网站的链接),那么必须包含BROWSABLE类别 |
CATEGORY_GADGET | The activity can be embedded inside of another activity that hosts gadgets. |
CATEGORY_HOME | 通过将一个Intent Filter的类别设置为HOME,而不指定一个action,就可以把它作为本地屏幕的可选项 |
CATEGORY_LAUNCHER | 使用这个类别会让一个Activity出现在应用程序的启动器中 |
CATEGORY_PREFERENCE | The target activity is a preference panel. |
CATEGORY_ALTERNATIVE | 可以把这个动作指定为在特定数据类型上执行的默认动作的可选项,例如,一个联系人的默认动作时查看其信息,而可选项的动作则是对其进行编辑 |
CATEGORY_SELECTED_ ALTERNATIVE | 与ALTERNATIVE相似,但是ALTERNATIVE总是使用后面将描述的intent resolution 解析为一个动作,而当要求有很多种可能性的时候,则可以使用SELECTED_ALTERNATIVE |
CATEGORY_DEFAULT | 通过设置这个类型可以使一个组件成为Intent Filter内制定的数据类型的默认动作。对于那些使用一个显示的Intent启动Activity,这个类型是很有必要的 |
(5)component(组件),指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。
(6)extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。
理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,指定要启动的Activity;另一种是隐式的Intent,即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,有利于降低发送者和接收者之间的耦合。
对于显式Intent,Android不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些隐式Intent,通过解析,将 Intent映射给可以处理此Intent的Activity、IntentReceiver或Service。
Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent,最终找到匹配的Intent。在这个解析过程中,Android是通过Intent的action、type、category这三个属性来进行判断的,判断方法如下:
· 如果Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;
· 如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。
· 如果Intent中的数据不是content: 类型的URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme (比如 http: 或者mailto:)进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中。
· 如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比如Intent中包含了两个类别:LAUNCHER_CATEGORY 和ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。
小例子
1) 下面的代码展示了一个Activity的IntentFilter,它将基于它的mime类型执行SHOW_DAMAGE动作,这个动作既可以是主要的,也可以是可选的
<intent-filter>
<action android:name="com.paad.earchquake.intent.action.SHOW_DAMAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.SELECTED_ALTERNATIVE"/>
<data android:mimeType="vnd.earthquake.cursor.item/*"/> </intent-filter>
2)在Android设备上点击YouTube视频或Google Map位置的链接时,会分别提示你使用YouTube或Google Map。这是通过在Intent Filter的data标签下指定scheme、host、path属性来实现的,下面的例子中,以http://blog.radioactiveyak.com形式开头的链接都将又、由这个Activity来处理。
<activity android:name=".MyBlogViewerActivity">
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="blog.radioactiveyak.com"/>
</intent-filter>
</activity>
确定Intent能否解析
Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:555-2368"));
//检查Activity是否存在
PackageManager pm=getPackageManager();
ComponentNamecn=intent.resolveActivity(pm);
if(cn==null){
//不存在Activity
//检查googleplay是否可用
UrimarketUri=Uri.parse("market://search?q=pname:com.myapp.packagename");
IntentmarketIntent=new Intent(Intent.ACTION_VIEW).setData(marketUri);
//google play可用
if(marketIntent.resolveActivity(pm)!=null)
startActivity(marketIntent);
}else{
startActivity(intent);
}
注意,必须包含有browsable类别,以便在浏览器中点击链接能够触发这种行为。
Intent-Filter的定义
一些属性设置的例子:
<actionandroid:name="com.example.project.SHOW_CURRENT" />
<categoryandroid:name="android.intent.category.DEFAULT" />
<dataandroid:mimeType="video/mpeg" android:scheme="http" . . ./>
<dataandroid:mimeType="image/*" />
<dataandroid:scheme="http" android:type="video/*" />
完整的实例
<activityandroid:name="NotesList"android:label="@string/title_notes_list">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<actionandroid:name="android.intent.action.VIEW"/>
<actionandroid:name="android.intent.action.EDIT"/>
<actionandroid:name="android.intent.action.PICK"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:mimeType="vnd.android.cursor.dir/vnd.google.note"/>
</intent-filter>
<intent-filter>
<actionandroid:name="android.intent.action.GET_CONTENT"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:mimeType="vnd.android.cursor.item/vnd.google.note"/>
</intent-filter>
</activity>
Intent用法实例
1.无参数Activity跳转
Intent it = newIntent(Activity.Main.this, Activity2.class);
startActivity(it);
2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)
Intent it = newIntent(Activity.Main.this, Activity2.class);
Bundle bundle=newBundle();
bundle.putString("name","This is from MainActivity!");
it.putExtras(bundle); // it.putExtra(“test”, "shuju”);
startActivity(it); //startActivityForResult(it,REQUEST_CODE);
对于数据的获取可以采用:
Bundlebundle=getIntent().getExtras();
Stringname=bundle.getString("name");
3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)
Intent intent=getIntent();
Bundle bundle2=new Bundle();
bundle2.putString("name","This is from ShowMsg!");
intent.putExtras(bundle2);
setResult(RESULT_OK, intent);
4.回调上一个Activity的结果处理函数(onActivityResult)
@Override
protected void onActivityResult(intrequestCode, 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;
Bundlebundle=data.getExtras();
if(bundle!=null) temp=bundle.getString("name");
setTitle(temp);
}
}
}
下面是转载来的其他的一些Intent用法实例(转自javaeye)
显示网页
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" />
传送SMS/MMS
1. //调用短信程序
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);
传送 Email
1. Uri uri = Uri.parse("mailto:xxx@abc.com");
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);
Market 相关
1. //寻找某个应用
2. Uri uri = Uri.parse("market://search?q=pname:pkg_name");
3. Intent it = new Intent(Intent.ACTION_VIEW, uri);
4. startActivity(it);
5. //where pkg_name is the full package path for an application
1. //显示某个应用的相关信息
2. Uri uri = Uri.parse("market://details?id=app_id");
3. Intent it = new Intent(Intent.ACTION_VIEW, uri);
4. startActivity(it);
5. //where app_id is the application ID, find the ID
6. //by clicking on your application on Market home
7. //page, and notice the ID from the address bar
Uninstall 应用程序
1. Uri uri = Uri.fromParts("package", strPackageName, null);
2. Intent it = new Intent(Intent.ACTION_DELETE, uri);
3. startActivity(it);