intent

http://blog.csdn.net/liuhe688/article/details/7050868

看似尋常最奇崛,成如easy卻艱辛。北宋.王安石

看似普通的事情事实上最不同平常,并非简简单单就能够做好的;成功看起来似乎非常easy,而成功的过程却充满着艰辛。

对于我们觉得非常普通的事情,不屑一顾,就永远不会有长进,脚踏实地,就离成功又近一步;成功并不像看到的那么easy,寻找捷径是不可取的,我们往往要比别人付出很多其它的辛勤和努力。

今天我们来讲一下Android中Intent的原理和应用。

前面我们总结了几个Android中重要组件,相信大家对于这些组件已经有了清晰的认识,我们就来看一下几个常见的操作:

启动一个Activity:Context.startActivity(Intent intent);

启动一个Service:Context.startService(Intent service);

绑定一个Service:Context.bindService(Intent service, ServiceConnection conn, int flags);

发送一个Broadcast:Context.sendBroadcast(Intent intent);

我们发现,在这些操作中,都有一个Intent參与当中,看起来像是一个很重要的组件,那么Intent究竟是什么呢?

简单来说,Intent是系统各组件之间进行数据传递的数据负载者。当我们须要做一个调用动作,我们就能够通过Intent告诉Android系统来完毕这个过程,Intent就是调用通知的一种操作。

Intent有几个重要的属性,以下我们将会逐一介绍:

1.action,要运行的动作

对于有例如以下声明的Activity:

  1. <activity android:name=".TargetActivity">  
  2.     <intent-filter>  
  3.         <action android:name="com.scott.intent.action.TARGET"/>  
  4.         <category android:name="android.intent.category.DEFAULT"/>  
  5.     </intent-filter>  
  6. </activity>  
TargetActivity在其<intent-filter>中声明了<action>,即目标action,假设我们须要做一个跳转的动作,就须要在Intent中指定目标的action,例如以下:
  1. public void gotoTargetActivity(View view) {  
  2.     Intent intent = new Intent("com.scott.intent.action.TARGET");  
  3.     startActivity(intent);  
  4. }  

当我们为Intent指定相应的action,然后调用startActivity方法后,系统会依据action跳转到相应的Activity。

除了自己定义的action之外,Intent也内含了非常多默认的action,随便列举几个:

  1. public static final String ACTION_MAIN = "android.intent.action.MAIN";  
  2. public static final String ACTION_VIEW = "android.intent.action.VIEW";  
  3. public static final String ACTION_WEB_SEARCH = "android.intent.action.WEB_SEARCH";  
  4. public static final String ACTION_CALL = "android.intent.action.CALL";  

每个action都有其特定的用途,下文也会使用到它们。

2.data和extras,即运行动作要操作的数据和传递到目标的附加信息

以下就举一个与浏览器交互的样例:

  1. /** 
  2.  * 打开指定网页 
  3.  * @param view 
  4.  */  
  5. public void invokeWebBrowser(View view) {  
  6.     Intent intent = new Intent(Intent.ACTION_VIEW);  
  7.     intent.setData(Uri.parse("http://www.google.com.hk"));  
  8.     startActivity(intent);  
  9. }  
  10.   
  11. /** 
  12.  * 进行关键字搜索 
  13.  * @param view 
  14.  */  
  15. public void invokeWebSearch(View view) {  
  16.     Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);  
  17.     intent.putExtra(SearchManager.QUERY, "android");    //关键字  
  18.     startActivity(intent);  
  19. }  
上面两个方法各自是启动浏览器并打开指定网页、进行keyword搜索,分别相应的action是Intent.ACTION_VIEW和Intent.ACTION_WEB_SEARCH,前者需指定相应的网页地址,后者需指定keyword信息,对于keyword搜索来说,浏览器会依照自己设置的默认的搜索引擎进行搜索。

我们注意到,在打开网页时,为Intent指定一个data属性,这事实上是指定要操作的数据,是一个URI的形式,我们能够将一个指定前缀的字符串转换成特定的URI类型,如:“http:”或“https:”表示网络地址类型,“tel:”表示电话号码类型,“mailto:”表示邮件地址类型,等等。比如,我们要呼叫给定的号码,能够这样做:

  1. public void call(View view) {  
  2.     Intent intent = new Intent(Intent.ACTION_CALL);  
  3.     intent.setData(Uri.parse("tel:12345678"));  
  4.     startActivity(intent);  
  5. }  

那么我们怎样知道目标是否接受这样的前缀呢?这就须要看一下目标中<data/>元素的匹配规则了。

在目标<data/>标签中包括了下面几种子元素,他们定义了url的匹配规则:

android:scheme 匹配url中的前缀,除了“http”、“https”、“tel”...之外,我们能够定义自己的前缀

android:host 匹配url中的主机名部分,如“google.com”,假设定义为“*”则表示随意主机名

android:port 匹配url中的端口

android:path 匹配url中的路径

我们修改一下TargetActivity的声明信息:

  1. <activity android:name=".TargetActivity">  
  2.     <intent-filter>  
  3.         <action android:name="com.scott.intent.action.TARGET"/>  
  4.         <category android:name="android.intent.category.DEFAULT"/>  
  5.         <data android:scheme="scott" android:host="com.scott.intent.data" android:port="7788" android:path="/target"/>  
  6.     </intent-filter>  
  7. </activity>  
这个时候假设仅仅指定action就不够了,我们须要为其设置data值,例如以下:
  1. public void gotoTargetActivity(View view) {  
  2.     Intent intent = new Intent("com.scott.intent.action.TARGET");  
  3.     intent.setData(Uri.parse("scott://com.scott.intent.data:7788/target"));  
  4.     startActivity(intent);  
  5. }  
此时,url中的每一个部分和TargetActivity配置信息中所有一致才干跳转成功,否则就被系统拒绝。

只是有时候对path限定死了也不太好,比方我们有这种url:(scott://com.scott.intent.data:7788/target/hello)(scott://com.scott.intent.data:7788/target/hi

这个时候该怎么办呢?我们须要使用另外一个元素:android:pathPrefix,表示路径前缀。

我们把android:path="/target"改动为android:pathPrefix="/target",然后就能够满足以上的要求了。

而在进行搜索时,我们使用了一个putExtra方法,将keyword做为參数放置在Intent中,我们成为extras(附加信息),这里面涉及到了一个Bundle对象。

Bundle和Intent有着密不可分的关系,主要负责为Intent保存附加參数信息,它实现了android.os.Paracelable接口,内部维护一个Map类型的属性,用于以键值对的形式存放附加參数信息。在我们使用Intent的putExtra方法放置附加信息时,该方法会检查默认的Bundle实例为不为空,假设为空,则新创建一个Bundle实例,然后将详细的參数信息放置到Bundle实例中。我们也能够自己创建Bundle对象,然后为Intent指定这个Bundle就可以,例如以下:

  1. public void gotoTargetActivity(View view) {  
  2.     Intent intent = new Intent("com.scott.intent.action.TARGET");  
  3.     Bundle bundle = new Bundle();  
  4.     bundle.putInt("id"0);  
  5.     bundle.putString("name""scott");  
  6.     intent.putExtras(bundle);  
  7.     startActivity(intent);  
  8. }  
须要注意的是,在使用putExtras方法设置Bundle对象之后,系统进行的不是引用操作,而是复制操作,所以假设设置完之后再更改bundle实例中的数据,将不会影响Intent内部的附加信息。那我们怎样获取设置在Intent中的附加信息呢?与之相应的是,我们要从Intent中获取到Bundle实例,然后再从中取出相应的键值信息:
  1. Bundle bundle = intent.getExtras();  
  2. int id = bundle.getInt("id");  
  3. String name = bundle.getString("name");  

当然我们也能够使用Intent的getIntExtra和getStringExtra方法获取,其数据源都是Intent中的Bundle类型的实例对象。

前面我们涉及到了Intent的三个属性:action、data和extras。除此之外,Intent还包含下面属性:

3.category,要运行动作的目标所具有的特质或行为归类

比如:在我们的应用主界面Activity通常有例如以下配置:

  1. <category android:name="android.intent.category.LAUNCHER" />  
代表该目标Activity是该应用所在task中的初始Activity而且出如今系统launcher的应用列表中。

几个常见的category例如以下:

Intent.CATEGORY_DEFAULT(android.intent.category.DEFAULT) 默认的category

Intent.CATEGORY_PREFERENCE(android.intent.category.PREFERENCE) 表示该目标Activity是一个首选项界面;

Intent.CATEGORY_BROWSABLE(android.intent.category.BROWSABLE)指定了此category后,在网页上点击图片或链接时,系统会考虑将此目标Activity列入可选列表,供用户选择以打开图片或链接。

在为Intent设置category时,应使用addCategory(String category)方法向Intent中加入指定的类别信息,来匹配声明了此类别的目标Activity。

4.type:要运行动作的目标Activity所能处理的MIME数据类型

比如:一个能够处理图片的目标Activity在其声明中包括这种mimeType:

  1. <data android:mimeType="image/*" />  
在使用Intent进行匹配时,我们能够使用setType(String type)或者setDataAndType(Uri data, String type)来设置mimeType。有一点要注意,假设单独设置TYPE或者DATA  最后一个设置的会把前面设置的清空,比如:
              intent.setData("file:///sdcard/image.data");
              intent.setType("image/jpg");
你会发现数据被清空了,反之。。。,假设想同一时候设置数据和类型就得用setDataAndType(Uri data, String type)

5.component,目标组件的包或类名称

在使用component进行匹配时,一般採用下面几种形式:

  1. intent.setComponent(new ComponentName(getApplicationContext(), TargetActivity.class));  
  2. intent.setComponent(new ComponentName(getApplicationContext(), "com.scott.intent.TargetActivity"));  
  3. intent.setComponent(new ComponentName("com.scott.other""com.scott.other.TargetActivity"));  

当中,前两种是用于匹配同一包内的目标,第三种是用于匹配其它包内的目标。须要注意的是,假设我们在Intent中指定了component属性,系统将不会再对action、data/type、category进行匹配。

注意:

inteng filter对象里面ACTION和CATEGORY都能够设置成多个,比如

假设一个页面有多个ACTION  比如:

页面2的

 <activity
            android:name="com.example.myfirstapp.SecondActivity"
            android:label="@string/title_activity_second" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.second"/>
                <action android:name="com.example.myfirstapp.three"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

我用sedIntent.setAction("com.example.myfirstapp.three");和sedIntent.setAction("com.example.myfirstapp.second");都能实现其跳转,即能找到该ACTIVITY

假设多个category也能够被找到,他们都算或的关系,可是,INTENT对象里面假设加入多个category的话,那么必须所有都被满足后才干够找到相应的ACTIVITY

比如:

        <activity
            android:name="com.example.myfirstapp.SecondActivity"
            android:label="@string/title_activity_second" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.second"/>
                <action android:name="com.example.myfirstapp.three"/>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.TAB" />
                <category android:name="android.intent.category.ALTERNATIVE" />
            </intent-filter>
        </activity>

我用               

                Intent sedIntent=new Intent();
                sedIntent.addCategory("android.intent.category.DEFAULT");
                sedIntent.addCategory("android.intent.category.ALTERNATIVE");
                sedIntent.setAction("com.example.myfirstapp.three");
                startActivity(sedIntent);

能够訪问该ACTIVITY,可是:
                Intent sedIntent=new Intent();
                sedIntent.addCategory("android.intent.category.DEFAULT");
                sedIntent.addCategory("android.intent.category.APP_BROWSER");
                sedIntent.setAction("com.example.myfirstapp.three");
                startActivity(sedIntent);
会报错,说页面找不到

另一种情况是
        <activity
            android:name="com.example.myfirstapp.SecondActivity"
            android:label="@string/title_activity_second" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.second"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.example.myfirstapp.three"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
随便用哪种方式去启动,仅仅有满足当中一个适配就能够找到。
sedIntent.setAction("com.example.myfirstapp.three");和sedIntent.setAction("com.example.myfirstapp.second");都能实现其跳转,即能找到该ACTIVITY




Intent的中文意思是“意图,目的”的意思,能够理解为不同组件之间通信的“媒介”或者“信使”。

 

目标组件一般要通过Intent来声明自己的条件,一般通过组件中的<intent-filter>元素来过滤。

 

Intent在由下面几个部分组成:动作(action),数据(data),分类(Category),类型(Type),组件(Component),和扩展信息(Extra)。

 

Intent在寻找目标组件的时候有两种方法:第一,通过组件名称直接指定;第二,通过Intent Filter过滤指定

 

 

Intent启动不同组件的方法

组件名称

方法名称

Activity

startActivity()

startActivityForResult()

Service

startService()

bindService()

Broadcasts

sendBroadcast()

sendOrderedBroadcast()

sendStickyBroadcast()

 

 

常见的Activity Action Intent常量

常量名称

常量值

意义

ACTION_MAIN

android.intent.action.MAIN

应用程序入口

ACTION_VIEW

android.intent.action.VIEW

显示数据给用户

ACTION_ATTACH_DATA

android.intent.action.ATTACH_DATA

指明附加信息给其它地方的一些数据

ACTION_EDIT

android.intent.action.EDIT

显示可编辑的数据

ACTION_PICK

android.intent.action.PICK

选择数据

ACTION_CHOOSER

android.intent.action.CHOOSER

显示一个Activity选择器

ACTION_GET_CONTENT

android.intent.action.GET_CONTENT

获得内容

ACTION_DIAL

android.intent.action.GET_CONTENT

显示打电话面板

ACITON_CALL

android.intent.action.DIAL

直接打电话

ACTION_SEND

android.intent.action.SEND

直接发短信

ACTION_SENDTO

android.intent.action.SENDTO

选择发短信

ACTION_ANSWER

android.intent.action.ANSWER

应答电话

ACTION_INSERT

android.intent.action.INSERT

插入数据

ACTION_DELETE

android.intent.action.DELETE

删除数据

ACTION_RUN

android.intent.action.RUN

执行数据

ACTION_SYNC

android.intent.action.SYNC

同步数据

ACTION_PICK_ACTIVITY

android.intent.action.PICK_ACTIVITY

选择Activity

ACTION_SEARCH

android.intent.action.SEARCH

搜索

ACTION_WEB_SEARCH

android.intent.action.WEB_SEARCH

Web搜索

ACTION_FACTORY_TEST

android.intent.action.FACTORY_TEST

工厂測试入口点

 

常见的BroadcastIntent Action常量

BroadcastIntent Action字符串常量

描写叙述

ACTION_TIME_TICK

系统时间每过一分钟发出的广播

ACTION_TIME_CHANGED

系统时间通过设置发生了变化

ACTION_TIMEZONE_CHANGED

时区改变

ACTION_BOOT_COMPLETED

系统启动完成

ACTION_PACKAGE_ADDED

新的应用程序apk包安装完成

ACTION_PACKAGE_CHANGED

现有应用程序apk包改变

ACTION_PACKAGE_REMOVED

现有应用程序apk包被删除

ACTION_UID_REMOVED

用户id被删除

 

Intent的Action和Data属性匹配

Action属性

Data属性

说明

ACTION_VIEW

content://contacts/people/1

显示id为1的联系人信息

ACTION_DIAL

content://contacts/people/1

将id为1的联系人电话号码显示在拨号界面中

ACITON_VIEW

tel:123

显示电话为123的联系人信息

ACTION_VIEW

http://www.google.com

在浏览器中浏览该站点

ACTION_VIEW

file://sdcard/mymusic.mp3

播放MP3

ACTION_VIEW

geo:39.2456,116.3523

显示地图

 

 

常见的Category常量

Category字符串常量

描写叙述

CATEGORY_BROWSABLE

目标Activity能通过在网页浏览器中点击链接而激活(比方,点击浏览器中的图片链接)

CATEGORY_GADGET

表示目标Activity能够被内嵌到其它Activity其中

CATEGORY_HOME

目标Activity是HOME Activity,即手机开机启动后显示的Activity,或按下HOME键后显示的Activity

CATEGORY_LAUNCHER

表示目标Activity是应用程序中最优先被运行的Activity

CATEGORY_PREFERENCE

表示目标Activity是一个偏爱设置的Activity

 

常见的Extra常量

Extra键值字符串常量

描写叙述

EXTRA_BCC

装有邮件密送地址的字符串数组

EXTRA_CC

装有邮件抄送地址的字符串数组

EXTRA_EMAIL

装有邮件发送地址的字符串数组

EXTRA_INTENT

使用ACTION_PICK_ACTIVITY动作时装有Intent选项的键

EXTRA_KEY_EVENT

触发该Intent的案件的KeyEvent对象

EXTRA_PHONE_NUMBER

使用拨打电话相关的Action时,电话号码字符串的键,类型为String

EXTRA_SHORTCUT_ICON

使用ACTION_CREATE_SHORTCUT在HomeActivity创建快捷方式时,对快捷方式的描写叙述信息。当中ICON和ICON_RESOURCE描写叙述的是快捷方式的图标,类型分别为Bitmap和ShortcutIconResource。INTENT描写叙述的是快捷方式相相应的Intent对象。NAME描写叙述的是快捷方式的名字。

EXTRA_SHORTCUT_ICON_RESOURCE

EXTRA_SHORTCUT_INTENT

EXTRA_SHORTCUT_NAME

EXTRA_SUBJECT

描写叙述信息主题的键

EXTRA_TEXT

使用ACTION_SEND动作时,用来描写叙述要发送的文本信息,类型为CharSequence

EXTRA_TITLE

使用ACTION_CHOOSER动作时,描写叙述对话框标题的键,类型为CharSequence

EXTRA_UID

使用ACTION_UID_REMOVED动作时,描写叙述删除的用户id的键,类型为int

 

Android.telephony包中的类

类名

描写叙述

CellLocation

表示设备位置的抽象类

PhoneNumberFormattingTextWather

监视一个TextView控件,假设有电话号码输入,则用formatNumber()方法处理电话号码

PhoneNumberUtils

包括各种处理电话号码字符串的使用工具

PhoneStateListener

监视手机中电话状态变化的监听类

ServiceState

包括电话状态和相关的服务信息

TelephonyManager

提供对手机中电话服务信息的訪问

 

与短信服务相关的类主要在包android.telephony.gsm中

类名

描写叙述

GsmCellLocation

表示GSM手机的基站位置

SmsManager

管理各种短信操作

SmsMessage

表示详细的短信



posted @ 2014-06-18 13:40  mengfanrong  阅读(349)  评论(0编辑  收藏  举报