Intent的Action、Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,而Category则用于为Action增加额外的附加列别的信息。通常Action属性会与Category属性结合使用。

Action要完成的只是一个抽象的动作,这个动作具体由哪个组件(或许是Activity,或许是BroadcastReceiver)来完成,Action这个字符串并不管。比如Android提供的标准Action:Intent.ACTION_VIEW,它只表示一个抽象的查看操作,但具体查看什么、启动哪个Activity来查看,Intent.ACTION_VIEW并不知道——这取决于Activity的<intent-filter.../>配置,只要某个Activity的<intent-filter.../>配置中包含了该ACTION_VIEW,该Activity就有可能被启动。

下面通过一个简单的示例来示范Action属性(就是普通字符串)的作用。下面的程序的第一个Activity非常简单,它只包括一个普通按钮,当用户单击该按钮时,程序会跳转到第二个Activity——但第一个Activity指定跳转的Intent时,并不以“硬编码”的方式指定要跳转的Activity,而是为Intent指定Action属性。

界面布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动第二个Activity"/>
</LinearLayout>

后台代码文件如下:

package com.example.studyintent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class ActionAttr extends Activity {
//定义一个Action常量
public final static String CRAZYIT_ACTION="com.example.studyintent.action.CRAZYIT_ACTION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_attr);
Button bn=(Button)findViewById(R.id.bn);
//为bn按钮绑定事件监听器
bn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建Intent对象
Intent intent=new Intent();
//为Intent设置Action属性(属性值就是一个普通字符串)
intent.setAction(ActionAttr.CRAZYIT_ACTION);
startActivity(intent);
}}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_attr, menu);
return true;
} }

由于上面的程序指定启动Action属性ActionAttr.CRAZYIT_ACTION常量(常量值为com.example.studyintent.action.CRAZYIT_ACTION)的Activity,也就要求被启动Activity对应的配置元素的<intent-filter.../>元素里至少包括一个如下的<action.../>子元素:

<action android:name="com.example.studyintent.action.CRAZYIT_ACTION"/>

需要指出的是,一个Intent对象最多只能包含一个Action属性,程序可调用Intent的setAction(String str)方法来设置Action属性值;但一个Intent对象可以包含多个Category属性,程序可调用Intent的addCategory(Stirng str)方法来为Intent添加Category属性。当程序创建Intent时,该Intent默认启动Category属性值为Intent.CATEGORY_DEFAULT常量(常量值为android.intent.category.DEFAULT)的组件。

因此,虽然上面程序的粗体字代码并未指定目标Intent的Category属性,但该Intent已有一个值为android.intent.category.DEFAULT的Category属性值,因此被启动Activity对应的配置元素的<intent-filter.../>元素里至少还包含一个如下的<category.../>子元素:

<category android:name="android.intent.category.DEFAULT" />

下面是被启动Activity的完整配置。

  <activity
android:name="com.example.studyintent.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter> <!-- 指定该Activity能响应Action为指定字符串的Intent -->
<action android:name="com.example.studyintent.action.CRAZYIT_ACTION" />
<!-- 指定该Activity能响应category为指定字符串的Intent -->
<category android:name="com.example.studyintent.category.CRAZYIT_CATEGORY" />
<!-- 指定该Activity能响应category为android.intent.category.DEFAULT的Intent -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

上面的配置代码中配置了一个实现类为SecondActivity的Activity,因此程序还提供了这个Activity的代码,代码如下:

package com.example.studyintent;

import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.view.Menu;
import android.widget.EditText; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); EditText show=(EditText)findViewById(R.id.show);
//获取该Activity对应的Intent的Action属性
String action=getIntent().getAction();
//显示Action属性
show.setText("Action为:"+action);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
} }

上面的程序代码很简单,它只是在启动时把启动该Activity的Intent的Action属性显示在指定文本框内。运行上面的程序并单击程序中的按钮,将看到如图5.2所示界面。

接下来的示例程序将会示范Category属性的用法,该程序的第一个Activity的代码如下。

package com.example.studyintent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class ActionCateAttr extends Activity {
//定义一个Action常量
final static String CRAZYIT_ACTION="com.example.studyintent.action.CRAZYIT_ACTION";
//定义一个Category常量
final static String CRAZYIT_CATEGORY="com.example.studyintent.category.CRAZYIT_CATEGORY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_cate_attr);
Button bn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
//设置Action属性
intent.setAction(ActionCateAttr.CRAZYIT_ACTION);
//添加Category属性
intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY);
startActivity(intent);
}});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_cate_attr, menu);
return true;
} }

下面是程序要启动的目标Action所对应的配置代码:

 <activity
android:name="com.example.studyintent.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter> <!-- 指定该Activity能响应Action为指定字符串的Intent -->
<action android:name="com.example.studyintent.action.CRAZYIT_ACTION" />
<!-- 指定该Activity能响应category为指定字符串的Intent -->
<category android:name="com.example.studyintent.category.CRAZYIT_CATEGORY" />
<!-- 指定该Activity能响应category为android.intent.category.DEFAULT的Intent -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

上面配置Activity时也指定该Activity的实现类为SecondActivity,该实现类的代码如下:

package com.example.studyintent;

import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.view.Menu;
import android.widget.EditText; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); EditText show=(EditText)findViewById(R.id.show);
//获取该Activity对应的Intent的Action属性
String action=getIntent().getAction();
//显示Action属性
show.setText("Action为:"+action);
EditText cate=(EditText)findViewById(R.id.cate);
//获取该Activity对应的Intent的Category
Set<String> cates=getIntent().getCategories();
//显示Category属性
cate.setText("Category属性为:"+cates); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
} }

运行上面程序单击程序中的按钮,将看到如图5.3所示的界面

Intent的属性及Intent-filter配置——Action、Category属性与intent-filter属性的更多相关文章

  1. web.xml之servlet与filter配置

    servlet配置 一个完整的servlet配置分为两块,< servlet >块和< servlet-mapping >块 < servlet > <ser ...

  2. Android开发之隐式Intent中Intent-filter的三个属性-action,category,data

    使用隐式Intent时,需要使用到意图过滤器Intent-filter.Intent-filter含有三个属性:action,category,data.通过这三个属性的组合,可以启动想要启动的act ...

  3. (转)android.intent.action.MAIN与android.intent.category.LAUNCHER

    android.intent.action.MAIN决定应用程序最先启动的Activity android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里 在网上看到 ...

  4. 理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER

    刚才看了一下sundy的视频<LLY110426_Android应用程序启动>,里面讲到luncher这个activity通过获取应用程序信息来加载应用程序,显示给用户,其中就是通过一个应 ...

  5. android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解 (转)

    原文地址:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解 作者: 第一种情况:有MAIN,无LAUNCHER,程 ...

  6. android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解

    第一种情况:有MAIN,无LAUNCHER,程序列表中无图标 原因:android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里  第二种情况:无MAIN,有LAU ...

  7. Android(java)学习笔记121:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解

    先看看网路上的说法: android.intent.action.MAIN决定应用程序最先启动的 Activity android.intent.category.LAUNCHER决定应用程序是否显示 ...

  8. android.intent.action.MAIN与android.intent.category.LAUNCHER

    android.intent.action.MAIN 决定应用程序最先启动的Activity android.intent.category.LAUNCHER 决定应用程序是否显示在程序列表里 在网上 ...

  9. Android(java)学习笔记62:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解

    1. 先看看网路上的说法: android.intent.action.MAIN 决定应用程序最先启动的 Activity android.intent.category.LAUNCHER 决定应用程 ...

随机推荐

  1. PowerDesigner设置线风格(直线,折线。。。)

    PowerDesigner中的绘图功能真是不敢恭维. 1.修改显示设置 Tools-->Display Preferences 这里有很多表现设置,我们需要的在Format菜单下. 点Modif ...

  2. 数据库建表的时候报 “1215 Cannot add foreign key constraint”

    很大原因是因为: 引用表中的字段类型和被引用的主键的类型不统一. 比如说学生表中有一个班级ID字段引用班级表. 班级表的ID是int类型,学生表中的班级ID是Varchar类型. 肯定会提示上述121 ...

  3. python数组查找算法---bisect二分查找插入

    1 实例 这个模块只有几个函数, 一旦决定使用二分搜索时,立马要想到使用这个模块 [python] view plaincopyprint? import bisect L = [1,3,3,6,8, ...

  4. google-c-style

    http://zhanxw.com/blog/2011/03/learning-and-applying-coding-style-from-google-in-emacs/ http://stack ...

  5. C语言数据类型的表示范围

    1.C和C++语言中基本的数据类型有:字符型(char),整形(short, int, long), 浮点型(float, double)    类型 字节数 类型 字节数 char 1 short ...

  6. CSS——京东首页实战总结

    第一天成果 1.浮动的盒子不要给宽,内容撑起盒子的宽 在前端设计中,一般不给浮动的盒子设置宽,让其用内容撑起一个高度. 2.小三角的表示 ◇用一个盒子(盒子宽为字的宽,高度为字高度的一半)去截取这个菱 ...

  7. iOS之PCH文件

    在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一个头文件,pch头文件的内容能被项目中的其他所有源文件 ...

  8. 11.10document对象练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  10. OPENCV图像变换-2

    一.经典霍夫变换 霍夫变换是图像处理中的一种特征提取技术,该方法通过在一个参数空间中通过计算累计结果的局部最大值来得到一个符合该特定形状的集合,作为结果. 运用两个坐标空间之间的变换,将一个空间中具有 ...