android中的Intent的用法和原理属性
详解Android应用开发中Intent的作用及使用方法

详解Android应⽤开发中Intent的作⽤及使⽤⽅法Intent是⼀种运⾏时绑定(run-time binding)机制,它能在程序运⾏过程中连接两个不同的组件。
通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。
⽐如,有⼀个Activity希望打开⽹页浏览器查看某⼀⽹页的内容,那么这个Activity只需要发出WEB_SEARCH_ACTION给Android,Android就会根据Intent的请求内容,查询各组件注册时声明的IntentFilter,找到⽹页浏览器的Activity来浏览⽹页。
Android的三个基本组件——Activity,Service和Broadcast Receiver——都是通过Intent机制激活的,不同类型的组件有不同的传递Intent⽅式:要激活⼀个新的Activity,或者让⼀个现有的Activity做新的操作,可以通过调⽤Context.startActivity()或者Activity.startActivityForResult()⽅法。
要启动⼀个新的Service,或者向⼀个已有的Service传递新的指令,调⽤Context.startService()⽅法或者调⽤Context.bindService()⽅法将调⽤此⽅法的上下⽂对象与Service绑定。
Context.sendBroadcast()、Context.sendOrderBroadcast()、Context.sendStickBroadcast()这三个⽅法可以发送Broadcast Intent。
发送之后,所有已注册的并且拥有与之相匹配IntentFilter的BroadcastReceiver就会被激活。
Intent⼀旦发出,Android都会准确找到相匹配的⼀个或多个Activity,Service或者BroadcastReceiver作响应。
Android+Intent机制实例详解

Android Intent机制实例详解(1)Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。
Intent这个英语单词的本意是“目的、意向”等,对于较少从事于大型平台开发工作的程序员来说,这可能是一个不太容易理解的抽象概念,因为它与我们平常使用的简单函数/方法调用,或者上节中提到的通过库调用接口的方式不太一样。
在Intent的使用中你看不到直接的函数调用,相对函数调用来说,Intent是更为抽象的概念,利用Intent所实现的软件复用的粒度是Activity/Service,比函数复用更高一些,另外耦合也更为松散。
Android中与Intent相关的还有Action/Category及Intent Filter等,另外还有用于广播的Intent,这些元素掺杂在一起,导致初学者不太容易迅速掌握Intent的用法。
在讲解这些名词之前,我们先来从下面的例子中感受一下Intent的一些基本用法,看看它能做些什么,之后再来思考这种机制背后的意义。
理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似,只是复用的粒度有所差别;另一种是隐式的Intent,即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,这种方式与函数调用差别比较大,有利于降低发送者和接收者之间的耦合。
另外Intent除了发送外,还可用于广播,这些都将在后文进行详细讲述。
下面的一小节我们来看看显式Intent的用法。
显式的Intent(Explicit Intent)同一个应用程序中的Activity切换我们在前面的章节已经讨论过Activity的概念,通常一个应用程序中需要多个UI屏幕,也就需要多个Activity类,并且在这些Activity之间进行切换,这种切换就是通过Intent机制来实现的。
intent在android中用法

intent在android中用法在Android中使用Intent的用法在Android开发中,Intent(意图)是一个非常重要的概念。
它可以用于应用程序组件之间的通信,包括Activity、Service、BroadcastReceiver 和ContentProvider。
Intent可以用于启动Activity、启动Service、发送广播以及在应用程序组件之间传递数据。
本篇文章将详细介绍在Android中使用Intent的用法,包括显式Intent和隐式Intent的使用、传递数据以及启动其他应用程序等。
一、显式Intent和隐式Intent的使用在Android中,可以使用显式Intent和隐式Intent来启动组件。
1.显式Intent的使用显式Intent指定了要启动的组件的类名。
下面是一个使用显式Intent启动Activity的示例代码:Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent);上述代码中,Intent的构造函数接收两个参数,第一个参数是当前Activity 的上下文对象,第二个参数是要启动的Activity的类名。
然后使用startActivity()方法启动Activity。
2.隐式Intent的使用隐式Intent是不指定目标组件的类名,而是通过设置Intent的Action、Category和Data等属性来匹配目标组件。
下面是一个使用隐式Intent 启动Activity的示例代码:Intent intent = new Intent();intent.setAction("com.example.action.START_SECOND_ACTIVITY"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent);上述代码中,首先创建了一个空的Intent对象,然后使用setAction()方法设置Action,使用addCategory()方法设置Category。
Intent的一些用法

Intent的一些用法(不断更新)文章分类:移动开发Intent用法实例1.无参数Activity跳转Java代码1.Intent it = new Intent(Activity.Main.this, Activity2.class);2.startActivity(it);2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)Java代码1.Intent it = new Intent(Activity.Main.this, Activity2.class);2.Bundle bundle=new Bundle();3.bundle.putString("name", "This is from MainActivity!");4.it.putExtras(bundle); // it.putExtra(“test”,"shuju”);5.startActivity(it); // startActivityForResult(it,REQUEST_CODE);对于数据的获取可以采用:Java代码1.Bundle bundle=getIntent().getExtras();2.String name=bundle.getString("name");3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)Java代码1.Intent intent=getIntent();2. Bundle bundle2=new Bundle();3. bundle2.putString("name", "This is from ShowMsg!");4. intent.putExtras(bundle2);5. setResult(RESULT_OK, intent);4.回调上一个Activity的结果处理函数(onActivityResult)Java代码1.@Override2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {3. // TODO Auto-generated method stub4. super.onActivityResult(requestCode, resultCode, data);5. if (requestCode==REQUEST_CODE){6. if(resultCode==RESULT_CANCELED)7. setTitle("cancle");8. else if (resultCode==RESULT_OK) {9. String temp=null;10. Bundle bundle=data.getExtras();11. if(bundle!=null) temp=bundle.getString("name");12. setTitle(temp);13. }14. }15. }下面是转载来的其他的一些Intent用法实例(转自javaeye)显示网页Java代码1.1. Uri uri = Uri.parse("");2.2. Intent it = new Intent(Intent.ACTION_VIEW, uri);3.3. startActivity(it);显示地图Java代码1.1. Uri uri = Uri.parse("geo:38.899533,-77.036476");2.2. Intent it = new Intent(Intent.ACTION_VIEW, uri);3.3. startActivity(it);4.4. //其他 geo URI 範例5.5. //geo:latitude,longitude6.6. //geo:latitude,longitude?z=zoom7.7. //geo:0,0?q=my+street+address8.8. //geo:0,0?q=business+near+city9.9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom路径规划Java代码1.1. Uri uri = Uri.parse("/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");2.2. Intent it = new Intent(Intent.ACTION_VIEW, uri);3.3. startActivity(it);4.4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456打电话Java代码1.1. //叫出拨号程序2.2. Uri uri = Uri.parse("tel:0800000123");3.3. Intent it = new Intent(Intent.ACTION_DIAL, uri);4.4. startActivity(it);5.1. //直接打电话出去6.2. Uri uri = Uri.parse("tel:0800000123");7.3. Intent it = new Intent(Intent.ACTION_CALL, uri);8.4. startActivity(it);9.5. //用這個,要在 AndroidManifest.xml 中,加上10. 6. //<uses-permission id="android.permission.CALL_PHONE" />传送SMS/MMSJava代码1.1. //调用短信程序2.2. Intent it = new Intent(Intent.ACTION_VIEW, uri);3.3. it.putExtra("sms_body", "The SMS text");4.4. it.setType("vnd.android-dir/mms-sms");5.5. startActivity(it);6.1. //传送消息7.2. Uri uri = Uri.parse("smsto://0800000123");8.3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);9.4. it.putExtra("sms_body", "The SMS text");10. 5. startActivity(it);11. 1. //传送 MMS12. 2. Uri uri = Uri.parse("content://media/external/images/media/23");13. 3. Intent it = new Intent(Intent.ACTION_SEND);14. 4. it.putExtra("sms_body", "some text");15. 5. it.putExtra(Intent.EXTRA_STREAM, uri);16. 6. it.setType("image/png");17. 7. startActivity(it);传送 EmailJava代码1.1. Uri uri = Uri.parse("mailto:xxx@");2.2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);3.3. startActivity(it);4.5.6.1. Intent it = new Intent(Intent.ACTION_SEND);7.2. it.putExtra(Intent.EXTRA_EMAIL, "me@");8.3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");9.4. it.setType("text/plain");10. 5. startActivity(Intent.createChooser(it, "Choose Email Client"));11.12.13. 1. Intent it=new Intent(Intent.ACTION_SEND);14. 2. String[] tos={"me@"};15. 3. String[] ccs={"you@"};16. 4. it.putExtra(Intent.EXTRA_EMAIL, tos);17. 5. it.putExtra(Intent.EXTRA_CC, ccs);18. 6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");19. 7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");20. 8. it.setType("message/rfc822");21. 9. startActivity(Intent.createChooser(it, "Choose Email Client"));传送附件Java代码1.Intent it = new Intent(Intent.ACTION_SEND);2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");4. sendIntent.setType("audio/mp3");5. startActivity(Intent.createChooser(it, "Choose Email Client"));播放多媒体Java代码1.Uri uri = Uri.parse("file:///sdcard/song.mp3");2. Intent it = new Intent(Intent.ACTION_VIEW, uri);3. it.setType("audio/mp3");4. startActivity(it);5. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");6. Intent it = new Intent(Intent.ACTION_VIEW, uri);7. startActivity(it);Market 相关//寻找某个应用Java代码1.Uri uri = Uri.parse("market://search?q=pname:pkg_name");2. Intent it = new Intent(Intent.ACTION_VIEW, uri);3. startActivity(it);//where pkg_name is the full package path for an applicationJava代码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 ID6. //by clicking on your application on Market home7. //page, and notice the ID from the address bar发起一个应用程序ComponentName 两个参数一个是包名一个是包下的主类Java代码1.final Intent intent = new Intent(Intent.ACTION_MAIN, null);2.intent.addCategory(Intent.CATEGORY_LAUNCHER);3.final ComponentName cn = new ComponentName("com.android.settings","com.android.settings.fuelgauge.PowerUsageSummary");4.intent.setComponent(cn);5.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);6.startActivity( intent);Uninstall 应用程序Java代码1.Uri uri = Uri.fromParts("package", strPackageName, null);2. Intent it = new Intent(Intent.ACTION_DELETE, uri);3. startActivity(it);返回桌面Java代码1.Intent unMyIntent = new Intent(Intent.ACTION_MAIN);2. unMyIntent.addCategory(Intent.CATEGORY_HOME);3. startActivity(unMyIntent);发邮件Java代码1.Uri uri = Uri.parse("mailto:fengsheng.studio@");2.Intent it = new Intent(Intent.ACTION_SENDTO, uri);发送文件Java代码1.Intent intent = new Intent();2.intent.setAction(Intent.ACTION_SEND);3.intent.setType(mimeType);4.intent.putExtra(Intent.EXTRA_STREAM, uri);5. startActivity(intent);6.7.以上有两个变量需要说明:8.1. mimeType: 如果是图片,则为"image/*",如果是音频,则为"audio/*",如果是视频,则为"video/*"9.2. uri:如果是直接读取到文件路径并发送,则为Uri.fromFile(new File(filePath));10. 如果是从数据库中查循出来的uri,则直接传入即可.发起市场程序Java代码1.Intent intent = new Intent(Intent.ACTION_VIEW,2. Uri.parse("market://details?id=pname:org.rabold.android.puzzleblox"));3. //Uri.parse("market://search?q=pname:org.rabold.android.puzzleblox"));4. startActivity(intent);。
Intent类型

Intent类型intent是⼀个消息传递对象,你可以使⽤它从其他应⽤组件请求操作。
intent的常⽤操作。
启动Activity activity已知是⼀个android的⼀个界⾯,通过调⽤startActivity(),我们可以启动⼀个新的activity,并且实现界⾯的跳转。
并且可以传递⼀些简单的数据。
如果我们希望能够从⼀个activity获取result的时候,我们可以调⽤startActivityForResult().这样,我们就可以启动⼀个新的activity,并且当这个新的activity执⾏完成后,我们可以得到⼀些返回的数据。
启动服务 service是⼀个不会使⽤⽤户界⾯,也就是在后台运⾏的⼀个线程或者进程。
将intent传递给startService(),我们可以启动⼀个服务完成⼀些操作,如播放⾳乐等。
intent的主要作⽤是⽤来设置要启动的服务过程,并且可以传递⼀些简单的数据。
传递⼴播 ⼴播式android的⼀个相当重要的组件,⼴播是任何应⽤都可以接受的消息。
系统针对系统事件传递各种⼴播。
通过intent传递给sendBroadcast(),sendOrderBroadcast()或者sendStickyBroadcast().,这样可以将⼴播传递给其他应⽤。
intent的使⽤分为两种类型 显式使⽤intent:明确指定activity来启动组件。
这样使⽤⽐较简单,但是有明确的要求就是,我们必须在我们需要明确的知道要启动的activity的类名。
创建显式intent启动activity和service,系统将⽴即启动intent对象中指定的应⽤组件。
代码:Intent intent=new Intent();intent.setClass(MainActivity.this, DisplayMessageActivity.class);EditText edit=(EditText)findViewById(R.id.edit1);String message=edit.getText().toString();intent.putExtra("message", message);startActivity(intent);显式使⽤intent 隐式使⽤intent:不特别指定要启动相应的activity的类名,⽽只要声明要执⾏的常规操作,从⽽允许其他应⽤中的组件来处理它。
intent原理及使用实验报告

intent原理及使用实验报告摘要:本实验报告主要介绍了intent的原理及其在Android开发中的使用。
通过实验,我们验证了intent在Android应用程序中的传递数据和启动其他组件的功能。
本文分为引言、实验设计、实验过程、实验结果、实验分析和结论六个部分。
1. 引言Intent是Android开发中的一个重要概念,用于在不同组件之间传递数据和启动其他组件。
Intent可以用来启动Activity、Service 和BroadcastReceiver等组件,也可以用来传递数据。
在Android 开发中,Intent被广泛使用,可以实现应用程序之间的交互和数据共享。
2. 实验设计本次实验的目的是验证intent在Android应用程序中的传递数据和启动其他组件的功能。
实验设计如下:(1)创建一个包含两个Activity的Android应用程序;(2)在第一个Activity中,通过Intent传递数据给第二个Activity;(3)在第二个Activity中,接收并显示传递的数据;(4)在第二个Activity中,通过Intent启动第一个Activity。
3. 实验过程(1)创建Android项目,并在AndroidManifest.xml文件中添加两个Activity的声明;(2)在第一个Activity中,创建一个按钮,点击按钮时触发事件;(3)在按钮的点击事件中,创建一个Intent对象,并通过putExtra()方法传递数据;(4)使用startActivity()方法启动第二个Activity;(5)在第二个Activity中,使用getIntent()方法获取传递的Intent对象,并通过getStringExtra()方法获取传递的数据;(6)在第二个Activity中,创建一个按钮,点击按钮时触发事件;(7)在按钮的点击事件中,创建一个Intent对象,并使用startActivity()方法启动第一个Activity。
安卓intent用法

安卓intent用法Android中的Intent是一种消息传递机制,用于在应用程序中传递信息或执行操作。
Intent可以用于在不同的组件之间启动服务、启动活动或者传递数据。
以下是一些示例用法:1. 启动应用程序组件可以使用Intent启动活动、服务或广播接收器。
例如,启动另一个活动可以使用以下代码:```Intent intent = new Intent(this, AnotherActivity.class); startActivity(intent);```其中,第一个参数是上下文,第二个参数是要启动的活动类。
2. 传递数据可以使用Intent传递数据,例如启动另一个活动,并且传递一些数据。
以下是传递字符串和整数的示例代码:```Intent intent = new Intent(this, AnotherActivity.class); intent.putExtra("message", "Hello World");intent.putExtra("number", 123);startActivity(intent);```在另一个活动中,可以使用以下代码获取所传递的数据:```Intent intent = getIntent();String message = intent.getStringExtra("message");int number = intent.getIntExtra("number", 0);```其中,getStringExtra()和getIntExtra()方法用于获取传递的字符串和整数。
3. 执行操作可以使用Intent执行某些操作,例如发送电子邮件或拨打电话。
以下是启动发送电子邮件的示例代码:```Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_EMAIL,"***************"); intent.putExtra(Intent.EXTRA_SUBJECT, "邮件的主题");intent.putExtra(Intent.EXTRA_TEXT, "邮件的正文");startActivity(Intent.createChooser(intent, "选择一个邮箱客户端"));```其中,setType()方法设置发送邮件的类型为纯文本,putExtra()方法设置电子邮件的相关信息,createChooser()方法创建一个选择器来选择邮箱客户端。
intent在android中的用法

intent在android中的用法在Android开发中,Intent是一种用于在应用程序组件之间传递信息的消息对象。
它可以用于启动活动(Activity)、服务(Service)或发送广播(Broadcast)。
以下是Intent在Android 中的几种常见用法:1.启动活动(Starting Activities):使用Intent启动一个新的活动。
例如,从当前活动跳转到另一个活动。
2.传递数据给活动(Passing Data to Activities):通过Intent的putExtra()方法,可以传递基本数据类型、字符串、序列化对象等。
3.返回数据给调用活动(Returning Data to the Calling Activity):使用startActivityForResult()启动新活动,并在新活动中使用setResult()设置返回数据。
4.启动服务(Starting Services):使用Intent启动服务,服务可以在后台执行长时间运行的操作。
5.发送广播(Sending Broadcasts):使用Intent发送广播,所有注册了相应广播接收器的组件都可以接收到广播。
6.隐式意图(Implicit Intents):不指定具体的组件类,而是声明要执行的操作。
系统会选择能够处理该操作的组件。
7.PendingIntent:PendingIntent是一种特殊的Intent,它允许你在稍后的某个时间点执行一个操作,或者由其他应用程序执行。
常用于通知、闹钟等场景。
8.使用Intent过滤器(Intent Filters):在AndroidManifest.xml中,使用<intent-filter>标签来声明活动、服务等可以响应哪些类型的Intent。
这对于定义应用程序的主入口点或响应系统事件非常有用。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
android中的Intent的用法和原理属性2010-06-10 17:18:52| 分类:android|字号订阅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中定义了一些标准的动作,包括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.ACTION_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 been turned on.ACTION_TIMEZONE_CHANGEDbroadcast receiverThe setting for the time zone has changed.当然,也可以自定义动作(自定义的动作在使用时,需要加上包名作为前缀,如"com.example.project.SHOW_COL OR”),并可定义相应的Activity来处理我们的自定义动作。
(2)Data,也就是执行动作要操作的数据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是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。
还有其他的为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 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_PREFERENCEThe target activity is a preference panel.(5)component(组件),指定Intent的的目标组件的类名称。
通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。
但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。
指定了这个属性以后,Intent的其它所有属性都是可选的。
(6)extras(附加信息),是其它所有附加信息的集合。
使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。
理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者;另一种是隐式的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,解析得到的目标组件必须至少包含这两个类别。
Intent-Filter的定义一些属性设置的例子:<action android:name="com.example.project.SHOW_CURRENT" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="video/mpeg" android:scheme="http" . . . /><data android:mimeType="image/*" /><data android:scheme="http" android:type="video/*" />完整的实例<activity android:name="NotesList" android:label="@string/title_notes_list"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="UNCHER" /></intent-filter><intent-filter><action android:name="android.intent.action.VIEW" /><action android:name="android.intent.action.EDIT" /><action android:name="android.intent.action.PICK" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /></intent-filter><intent-filter><action android:name="android.intent.action.GET_CONTENT" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter></activity>Intent用法实例1.无参数Activity跳转Intent it = new Intent(Activity.Main.this, Activity2.class);startActivity(it);2.向下一个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(bundle); // it.putExtra(“test”, "shuju”);startActivity(it); // startActivityForResult(it,REQUEST_CODE);对于数据的获取可以采用:Bundle bundle=getIntent().getExtras();String name=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)@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stubsuper.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用法实例(转自javaeye)显示网页1. Uri uri = Uri.parse("");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,longitude6. //geo:latitude,longitude?z=zoom7. //geo:0,0?q=my+street+address8. //geo:0,0?q=business+near+city9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom路径规划1. Uri uri =Uri.parse("/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/MMS1. //调用短信程序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. //传送 MMS2. 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);传送 Email1. Uri uri = Uri.parse("mailto:xxx@");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@");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@"};3. String[] ccs={"you@"};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 application1. //显示某个应用的相关信息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 ID6. //by clicking on your application on Market home7. //page, and notice the ID from the address barUninstall 应用程序1. Uri uri = Uri.fromParts("package", strPackageName, null);2. Intent it = new Intent(Intent.ACTION_DELETE, uri);3. startActivity(it);。