android中的Intent的用法和原理属性_第1页
android中的Intent的用法和原理属性_第2页
android中的Intent的用法和原理属性_第3页
android中的Intent的用法和原理属性_第4页
android中的Intent的用法和原理属性_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、android中的Intent的用法和原理属性2010-06-10 17:18:52| 分类:andoid|字号订阅Android中提供了 Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的 动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对 应的组件,将Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序 之间,也可用于应用程序内部的Activity/Service之间的交互。因此,Intent在这里起着一 个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解 耦。在SDK中

2、给出了 Intent作用的表现形式为:通过 Context.startActivity() orActivity.startActivityForResult()启动一个 Activity;通过Context.startService()启动一个服务,或者通过Context.bindService()和后台服务交 互;通过广播方法(比如 Context.sendBroadcast(),Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()发给 broadcast receivers0Intent属性的设置,包括以下几点:(以下

3、为XML中定义,当然也可以通过Intent类的方 法来获取和设置)(1) Action,也就是要执行的动作SDk中定义了一些标准的动作,包括onstantTarget componentActionACTION_CALLactivityInitiate a phone call.ACTION_EDITactivityDisplay data for the user to edit.ACTION_MAINactivityStart up as the initial activity of a task, with no data input and no returned output.AC

4、TION_SYNCactivitySynchronize data on a server with data on the mobile device. ACTION_BATTERY_LOWbroadcast receiverA warning that the battery is low.ACTION_HEADSET_PLUGbroadcast receiverA headset has been plugged into the device, or unplugged from it.ACTION_SCREEN_ONbroadcast receiverThe screen has b

5、een turned on.ACTION_TIMEZONE_CHANGEDbroadcast receiverThe setting for the time zone has changed.当然,也可以自定义动作(自定义的动作在使用时,需要加上包名作为前缀,如 ject.SHOW_COLOR”),并可定义相应的Activity来处理我们的自定义 动作。(2)Data,也就是执行动作要操作的数据Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的 URI可能为:content:/contacts/1。对于不同的动作,其URI数据的类型是不同的(可以 设置typ

6、e属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为 tel:URI,访问网络为 http:URI,而由 content provider提供的数据则为 content: URIs0(3)type (数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型 能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不 再进行推导。(4)category (类别),被执行动作的附加信息。例如LAUNCHER_CATEGORY表示 Intent的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CA

7、TEGORY 表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。还 有其他的为ConstantMeaningCATEGORY_BROWSABLEThe target activity can be safely invoked by the browser to display data referenced by a link for example, an image or an e-mail message.CATEGORY_GADGETThe activity can be embedded inside of another activity that

8、 hosts gadgets.CATEGORY_HOMEThe activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.CATEGORY_LAUNCHERThe activity can be the initial activity of a task and is listed in the top-level application launcher.CATEGORY_PREFERENCETh

9、e target activity is a preference panel.(5)component (组件),指定Intent的的目标组件的类名称。通常Android会根据 Intent中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到 一个与之匹配的目标组件。但是,如果component这个属性有指定的话,将直接使用它指 定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是 可选的。(6)extras (附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展 信息,比如,如果要执行“发送电子

10、邮件”这个动作,可以将电子邮件的标题、正文等保存 在extras里,传给电子邮件发送组件。理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构 造Intent对象时就指定接收者;另一种是隐式的Intent,即Intent的发送者在构造Intent 对象时,并不知道也不关心接收者是谁,有利于降低发送者和接收者之间的耦合。对于显式Intent,Android不需要去做解析,因为目标组件已经很明确,Android需要解析 的是那些隐式Intent,通过解析,将Intent映射给可以处理此Intent的Activity、IntentReceiver 或 Se

11、rvice 0Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中 定义的Intent,最终找到匹配的Intent。在这个解析过程中,Android是通过Intent的 action、type、category这三个属性来进行判断的,判断方法如下:如果Intent指明定了 action,则目标组件的IntentFilter的action列表中就必须包含有这个 action,否则不能匹配;如果Intent没有提供type,系统将从data中得到数据类型。和action 一样,目标组件的 数据类型列表中必须包含Intent的数据类型

12、,否则不能匹配。如果Intent中的数据不是content:类型的URI,而且Intent也没有明确指定它的type,将 根据Intent中数据的scheme (比如http:或者mailto:)进行匹配。同上,Intent的 scheme必须出现在目标组件的scheme列表中。如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比 如 Intent 中包含了两个类别:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY, 解析得到的目标组件必须至少包含这两个类别。Intent-Filter 的定义一些属性设置的例子:完整的实例

13、Intent用法实例无参数Activity跳转Intent it = new Intent(Activity.Main.this, Activity2.class);startActivity(it);向下一个 Activity 传递数据(使用 Bundle 和 Intent.putExtras)Intent it = new Intent(Activity.Main.this, Activity2.class);Bundle bundle=new Bundle();bundle.putString(name”, This is from MainActivity!);it.putExtras

14、(bundle); / it.putExtra(test”,shuju”);startActivity(it); / startActivityForResult(it,REQUEST_CODE);对于数据的获取可以采用:Bundle bundle=getIntent().getExtras();String name=bundle.getString(name);向上一个Activity返回结果(使用setResult,针对 startActivityForResult(it,REQUEST_CODE)启动的 Activity)Intent intent=getIntent();Bundle

15、 bundle2=new Bundle();bundle2.putString(name”, This is from ShowMsg!);intent.putExtras(bundle2);setResult(RESULT_OK, intent);回调上一个Activity的结果处理函数(onActivityResult)Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) / TODO Auto-generated method stub super.onActivityR

16、esult(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用法实例(转自javaeye)显示网页Uri uri =

17、Uri.parse( HYPERLINK );Intent it = new Intent(Intent.ACTION_VIEW, uri);startActivity(it);显示地图Uri uri = Uri.parse(geo:38.899533,-77.036476);Intent it = new Intent(Intent.ACTION_VIEW, uri);startActivity(it);/其他geo URI簸例/geo:latitude,longitude/geo:latitude,longitude?z=zoom/geo:0,0?q=my+street+address/g

18、eo:0,0?q=business+near+city/google.streetview:cbll=lat,lng&cbp=1,yaw,pitch,zoom&mz=mapZoom路径规划Uri uri =Uri.parse( HYPERLINK /maps?f=d&saddr=startLat%20startLng&daddr=endLat /maps?f=d&saddr=startLat%20startLng&daddr=endLat %20endLng&hl=en);Intent it = new Intent(Intent.ACTION_VIEW, uri);startActivity

19、(it);/where startLat, startLng, endLat, endLng are a long with 6 decimals like:50.123456打电话叫出拨号程序Uri uri = Uri.parse(tel:0800000123);Intent it = new Intent(Intent.ACTION_DIAL, uri);startActivity(it);/直接打电话出去Uri uri = Uri.parse(tel:0800000123);Intent it = new Intent(Intent.ACTION_CALL, uri);startActi

20、vity(it);/用逊固,要在 AndroidManifest.xml 中,加上/ 传送 SMS/MMS调用短信程序Intent it = new Intent(Intent.ACTION_VIEW, uri);it.putExtra(sms_body”, The SMS text);it.setType(vnd.android-dir/mms-sms);startActivity(it);传送消息Uri uri = Uri.parse(smsto:/0800000123);Intent it = new Intent(Intent.ACTION_SENDTO, uri);it.putExt

21、ra(sms_body”, The SMS text);startActivity(it);传送MMSUri uri = Uri.parse(content:/media/external/images/media/23);Intent it = new Intent(Intent.ACTION_SEND);it.putExtra(sms_body, some text);it.putExtra(Intent.EXTRA_STREAM, uri);it.setType(image/png);startActivity(it);传送EmailUri uri = Uri.parse( HYPERL

22、INK mailto:xxx mailto:xxx);Intent it = new Intent(Intent.ACTION_SENDTO, uri);startActivity(it);Intent it = new Intent(Intent.ACTION_SEND);it.putExtra(Intent.EXTRA_EMAIL,);it.putExtra(Intent.EXTRA_TEXT, The email body text);it.setType(text/plain);startActivity(Intent.createChooser(it, Choose Email Cl

23、ient);Intent it=new Intent(Intent.ACTION_SEND);String tos= HYPERLINK mailto:me me;String ccs= HYPERLINK mailto:you you;it.putExtra(Intent.EXTRA_EMAIL, tos);it.putExtra(Intent.EXTRA_CC, ccs);it.putExtra(Intent.EXTRA_TEXT, The email body text);it.putExtra(Intent.EXTRA_SUBJECT, The email subject text);

24、it.setType(message/rfc822”);startActivity(Intent.createChooser(it, Choose Email Client);传送附件Intent it = new Intent(Intent.ACTION_SEND);it.putExtra(Intent.EXTRA_SUBJECT, The email subject text);it.putExtra(Intent.EXTRA_STREAM, HYPERLINK sdcard/mysong.mp3 file:/sdcard/mysong.mp3);sendIntent.setType(audio/mp3);startActivity(Intent.createChooser(it, Choose Email Client);播放多媒体Uri uri = Uri.parse( HYPERLINK sdcard/song.mp3 file:/sdcard/song.mp3);Intent it

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论