Android之Intent

Android之Intent

.Intent的介绍

  Intent的中文意思是意图,意向,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的媒介专门提供组件互相调用的相关信息。

.Inten启动组件的方法

  Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcasts。具体方法如下:

组件名称

方法名称

Activity

startActvity( )

startActivity( )

Service

startService( )

bindService( )

Broadcasts

sendBroadcasts( )

sendOrderedBroadcasts( )

sendStickyBroadcasts( )

 


.Intent的属性

  Intent有以下几个属性:

  动作(Action),数据(Data),分类(Category),类型(Type),组件(Compent)以及扩展信(Extra)。其中最常用的是Action属性和Data属性。

  1.IntentAction属性

  Action是指Intent要完成的动作,是一个字符串常量。SDK中定义了一些标准的Action常量如下表所示。

Constant

Target component

Action

ACTION_CALL

activity

Initiate a phone call.

ACTION_EDIT

activity

Display data for the user to edit.

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.


  2.IntentData属性

  IntentData属性是执行动作的URIMIME类型,不同的Action有不同的Data数据指定。比如:ACTION_EDIT Action应该和要编辑的文档URI Data匹配,ACTION_VIEW应用应该和要显示的URI匹配。

  3.IntentCategory属性

  Intent中的Category属性是一个执行动作Action的附加信息。比如:CATEGORY_HOME则表示放回到Home界面,ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个。下表是SDK文档中关于Category的信息。

Constant

Meaning

CATEGORY_BROWSABLE

The 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_GADGET

The activity can be embedded inside of another activity that hosts gadgets.

CATEGORY_HOME

The 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_LAUNCHER

The activity can be the initial activity of a task and is listed in the top-level application launcher.

CATEGORY_PREFERENCE

The target activity is a preference panel.

  4.IntentType属性

  IntentType属性显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

  5.IntentCompent属性

  IntentCompent属性指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如actiondata/typecategory进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

  6.IntentExtra属性

  IntentExtra属性是添加一些组件的附加信息。比如,如果我们要通过一个Activity来发送一个Email,就可以通过Extra属性来添加subjectbody

 

四.Intent详细介绍

  Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。

  Intent传递过程中,要找到目标消费者(另一个Activity,IntentReceiver或Service),也就是Intent的响应者,有两种方法来匹配:

1.显示匹配(Explicit): 

public void switchActivity() { 
    Intent i = new Intent(Test.this, TestB.class);
    this.startActivity(i); 
}

2.隐式匹配(Implicit):

  隐式匹配首先要匹配Intent的几项值:Action, Category, Data/Type,Component如果填写了Componet就是上例中的Test.class)这就形成了显示匹配。所以此部分只讲前几种匹配。匹配规则为最大匹配规则。

1.如果你填写了Action,如果有一个程序的Manifest.xml中的某一个Activity的IntentFilter段中定义了包含了相同的 Action那么这个Intent就与这个目标Action匹配,如果这个Filter段中没有定义Type,Category,那么这个 Activity就匹配了。但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。Action的值在Android中有很多预定义,如果你想直接转到你自己定义的Intent接收者,你可以在接收者的IntentFilter中加入一个自定义的Action值(同时要设定Category值为"android.intent.category.DEFAULT"),在你的Intent 中设定该值为Intent的Action,就直接能跳转到你自己的Intent接收者中。因为这个Action在系统中是唯一的。

2.data/type,你可以用Uri来做为data,比如

Uri uri = Uri.parse(http://www.google.com );

Intent i = new Intent(Intent.ACTION_VIEW,uri);

手机的Intent分发过程中,会根据http://www.google.com 的scheme判断出数据类型type手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能处理http:的type。

3,至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的 IntentFilter中包含android.category.DEFAULT,这样所有不设置 Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。

4.extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

一、抽象描述要描述什么

  在Android参考文档中,对Intent的定义是执行某操作的一个抽象描述(确实很抽象)。我们先来看看这里的抽象描述,到底描述了什么。

首先,是要执行的动作(action)的一个简要描述,如VIEW_ACTION(查看)、EDIT_ACTION(修改)等,Android为我们定义了一套标准动作:

  ◆MAIN_ACTION

  ◆VIEW_ACTION

  ◆EDIT_ACTION

  ◆PICK_ACTION

  ◆GET_CONTENT_ACTION

  ◆DIAL_ACTION

  ◆CALL_ACTION

  ◆SENDTO_ACTION

  ◆ANSWER_ACTION

  ◆INSERT_ACTION

  ◆DELETE_ACTION

  ◆RUN_ACTION

  ◆LOGIN_ACTION

  ◆CLEAR_CREDENTIALS_ACTION

  ◆SYNC_ACTION

  ◆PICK_ACTIVITY_ACTION

  ◆WEB_SEARCH_ACTION

  此外,我们还可以根据应用的需要,定义我们自己的动作,并可定义相应的Activity来处理我们的自定义动作。

  其次,是执行动作要操作的数据(data),Android中 采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。这种URI表示,通过 ContentURI这个类来描述,具体可以参考android.net.ContentURI类的文档。

  以联系人应用为例,以下是一些action / data对,及其它们要表达的意图:

  ◆VIEW_ACTION content://contacts/1 -- 显示标识符为"1"的联系人的详细信息

  ◆EDIT_ACTION content://contacts/1 -- 编辑标识符为"1"的联系人的详细信息

  ◆VIEW_ACTION content://contacts/ -- 显示所有联系人的列表

  PICK_ACTION content://contacts/ -- 显示所有联系人的列表,并且允许用户在列表中选择一个联系人,然后把这个联系人返回给父activity。例如:电子邮件客户端可以使用这个Intent,要求用户在联系人列表中选择一个联系人

  另外,除了actiondata这两个重要属性外,还有一些附加属性:

  ◆category(类别),被执行动作的附加信息。例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这 些动作可以在同一块数据上执行。

  ◆type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

  ◆component(组件),指定Intent的的目标组件的类名称。通常Android会根据Intent 中包含的其它属性的信息,比如actiondata/typecategory进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

  ◆extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行发送电子邮件这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

二、Android如何解析Intent

  在应用中,我们可以以两种形式来使用Intent

  直接Intent:指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context, Class)来指定)。通过指定具体的组件类,通知应用启动对应的组件。间接Intent:没有指定comonent属性的Intent。这些Intent需要包含足够的信息,这样系统才能根据这些信息,在在所有的可用组件中,确定满足此Intent的组件。

  对于直接IntentAndroid不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些间接Intent,通过解析,将 Intent映射给可以处理此IntentActivityIntentReceiverService

  Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent, 最终找 到匹配的Intent。在这个解析过程中,Android是通过Intentactiontypecategory这三个属性来进行判断的,判断方 法如下:

  如果Intent指明定了action,则目标组件的IntentFilteraction列表中就必须包含有这个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 用法汇总 

 显示网页  

  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);  

posted @ 2012-12-11 10:46  JD.Tang  阅读(280)  评论(0编辑  收藏  举报