10

基础总结篇之九:Intent应用详解

 3 years ago
source link: http://www.androidchina.net/1002.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
基础总结篇之九:Intent应用详解 – Android开发中文站
你的位置:Android开发中文站 > Android开发 > 新手入门 > 基础总结篇之九:Intent应用详解

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

看似普通的事情其实最不同寻常,并不是简简单单就可以做好的;成功看起来似乎很容易,而成功的过程却充满着艰辛。

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

今天我们来讲一下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:

<activity android:name=".TargetActivity">
<intent-filter>
<action android:name="com.scott.intent.action.TARGET"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

TargetActivity在其<intent-filter>中声明了<action>,即目标action,如果我们需要做一个跳转的动作,就需要在Intent中指定目标的action,如下:

public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
startActivity(intent);
}

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

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

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

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

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

下面就举一个与浏览器交互的例子:

/**
* 打开指定网页
* @param view
*/
public void invokeWebBrowser(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com.hk"));
startActivity(intent);
}
/**
* 进行关键字搜索
* @param view
*/
public void invokeWebSearch(View view) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "android");    //关键字
startActivity(intent);
}

上面两个方法分别是启动浏览器并打开指定网页、进行关键字搜索,分别对应的action是Intent.ACTION_VIEW和Intent.ACTION_WEB_SEARCH,前者需指定相应的网页地址,后者需指定关键字信息,对于关键字搜索来说,浏览器会按照自己设置的默认的搜索引擎进行搜索。

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

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

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

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

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

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

android:port 匹配url中的端口

android:path 匹配url中的路径

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

<activity android:name=".TargetActivity">
<intent-filter>
<action android:name="com.scott.intent.action.TARGET"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="scott" android:host="com.scott.intent.data" android:port="7788" android:path="/target"/>
</intent-filter>
</activity>

这个时候如果只指定action就不够了,我们需要为其设置data值,如下:

public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
intent.setData(Uri.parse("scott://com.scott.intent.data:7788/target"));
startActivity(intent);
}

此时,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方法,将关键字做为参数放置在Intent中,我们成为extras(附加信息),这里面涉及到了一个Bundle对象。

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

public void gotoTargetActivity(View view) {
Intent intent = new Intent("com.scott.intent.action.TARGET");
Bundle bundle = new Bundle();
bundle.putInt("id"0);
bundle.putString("name""scott");
intent.putExtras(bundle);
startActivity(intent);
}

需要注意的是,在使用putExtras方法设置Bundle对象之后,系统进行的不是引用操作,而是复制操作,所以如果设置完之后再更改bundle实例中的数据,将不会影响Intent内部的附加信息。那我们如何获取设置在Intent中的附加信息呢?与之对应的是,我们要从Intent中获取到Bundle实例,然后再从中取出对应的键值信息:

Bundle bundle = intent.getExtras();
int id = bundle.getInt("id");
String name = bundle.getString("name");

当然我们也可以使用Intent的getIntExtra和getStringExtra方法获取,其数据源都是Intent中的Bundle类型的实例对象。
前面我们涉及到了Intent的三个属性:action、data和extras。除此之外,Intent还包括以下属性:
3.category,要执行动作的目标所具有的特质或行为归类
例如:在我们的应用主界面Activity通常有如下配置:

<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:

<data android:mimeType="image/*" />

在使用Intent进行匹配时,我们可以使用setType(String type)或者setDataAndType(Uri data, String type)来设置mimeType。
5.component,目标组件的包或类名称
在使用component进行匹配时,一般采用以下几种形式:

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

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

今天就先讲到这里吧,有时间的话再加以补充。

转载请注明:Android开发中文站 » 基础总结篇之九:Intent应用详解


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK