Intent有几个重要的属性,下面我们将会逐一介绍:
1.action,要执行的动作
除了自定义的Action外,Android还提供了几种内置的intent。
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";
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);
}
那么我们如何知道目标是否接受这种前缀呢?这就需要看一下目标中<data/>元素的匹配规则了。
在目标<data/>标签中包含了以下几种子元素,他们定义了url的匹配规则:
android:scheme 匹配url中的前缀,除了“http”、“https”、“tel”...之外,我们可以定义自己的前缀
android:host 匹配url中的主机名部分,如“google.com”,如果定义为“*”则表示任意主机名
android:port 匹配url中的端口
android:path 匹配url中的路径
此时,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值,如下:
[java] view plaincopy
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);
}
我们成为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);
}
前面我们涉及到了Intent的三个属性:action、data和extras。除此之外,Intent还包括以下属性:
3.category,要执行动作的目标所具有的特质或行为归类
<category android:name="android.intent.category.LAUNCHER" />
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列入可选列表,供用户选择以打开图片或链接。4.type:要执行动作的目标Activity所能处理的MIME数据类型
在使用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进行匹配。