Intent 和Intent-filter
2015-12-25 22:42
579 查看
今天上午有个朋友去面试,考题里就出到了Intent的用法,显示意图和隐式意图,还有如何用Intent传递数据。
那么我们就来看看这个Intent吧。
Intent封装Android应用程序需要启动某个组件的“意图”。Intent是应用程序组件之间通信的重要媒介。
Android的应用程序包含三种重要的组件:Activity,Service,BroadcastReceiver,都是依靠Intent来启动的,Intent还可用于与被启动的组件交换消息。我前面就写过Intent在Activity之间传递数据的文章。
http://blog.csdn.net/pangrongxian/article/details/50166835
Intent对象包含Component、Action、Category、Data、Type、Extra和Flag这7种属性。
为了演示,我们假设创建两个Activity,FirstActivity和SecondActivity。
1.Component(组件)目的组件
假设现在要从FirstActivity跳转到SecondActivity中,下面是代码:
//创建一个ComponentName对象
ComponentName comp = new ComponentName(FirstActivity.this , SecondActivity.class);
Intent intent = new Intent();
//为Intent设置Component属性
Intent.setComponent(comp);
startActivity();
上面程序的代码创建ComponentName对象,并将该对象设置成Intent对象的Component属性,这样应用程序即可根据该Intent的“意图”去启动指定的组件了。(这个例子指定的组件是:SecondActivity)
上面的代码可以简写成:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
指定Component属性的Intent已经明确了它将要启动那个组件,因此这种Intent也被称为显示Intent,没有指定Component属性的Intent被称为隐式Intent——隐式Intent没有明确指定要启动那个组件,应用程序会根据Intent指定的规则去启动符合条件的组件,那些是符合条件的组件呢?下面说道到的Action属性就是了。
2.Action(动作):用来表示意图的动作
3.Category(类别):用于表示动作的类别
Intent的Action、Category属性都是一个普通的字符串而已,Action代表该Intent所要完成的一个抽象的动作,而Category则作为Action增加额外的附加条件。
Action属性和Category属性在哪里写呢?它们两个都是在Manifest.xml清单文件中<activity>中的intent-filter中写的:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<strong><action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在Intent类中定义了一批Action(动作),例如:
action常量 对应字符串 说明
ACTION_MAIN android.intent.action.MAIN ——应用程序入口
ACTION_VIEW android.intent.action.VIEW ——查看指定数据
ACTION_CALL android.intent.action.CALL ——直接向指定用户打电话
ACTION_SEND android.intent.action.SEND ——向其他人发送数据
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属性,通过action属性来查找组件SecondActivity。
用Intent的隐式意图跳转到另一个Activity
action字符串的是可以由你任意来写的,一般把action字符串的写成:包名.action.MY_ACTION
在FirstActivity中写如下代码:
Intent intent = new Intent();
setAction(包名.action.MY_ACTION);
startActivity(intent);
然后在Manifest.xml清单文件中SecondActivity的<activity>中的<intent-filter>中配置action属性
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<strong><action android:name="包名.action.MY_ACTION" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
只要FirstActivity中setAction()方法中,括号中参数(即action属性的字符串)对应上Manifest.xml清单文件中SecondActivity的<activity>中的<intent-filter>中action属性的字符串,FirstActivity就能够通过action属性找到SecondActivity,然后通过Intent跳转到SecondActivity。
这种通过action属性去启动组件的方式称为隐式Intent(没有指定Component属性的Intent被称为隐式Intent)
注意:在<intent-filter>中,必须添加category 属性才行,category.LAUNCHER是默认的category 。
4.Data(数据):表示与动作要操作的数据
5.Type(数据类型):对于data数据范例的描述
6.Extra(扩展信息)
7.Flag(标志位)
其他的几个属性不是很常用,用到的时候再查资料就可以啦!
这里也有视频讲解这些属性的用法的,给一个扣丁学堂的连接:
http://www.codingke.com/course/167/learn#lesson/455
这篇就讲些纯理论的东西吧,我自己也不太喜欢太过理论的东西,还是比较喜欢实战。
最后来看看Intent 的API文档的简介:(我只是顺便学习一下英文的o( ̄ヘ ̄o#))
An
a messaging object you can use to request an action from another app
component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases:
一个Intent(意图)是一个消息对象,它通常能让你从另一个应用程序组件中请求一个动作。尽管Intents在好几个方面使应用程序组件之间的交流变得容易。这里有三个基本的使用例子:
To start an activity:启动一个Activity:
An
a single screen in an app. You can start a new instance of an
passing an
一个Activity代表一个应用程序中的一个屏幕(一个界面)。你可以通过传递一个Intent来startactivity(),启动一个新的activity实例。
The
the activity to start and carries any necessary data.
Intent(意图)描述Activity的启动和携带任何必要的数据。
If you want to receive a result from the activity when it finishes, call
当一个activity结束掉的时候,如果你想要接收到一个activity返回的结果,你需要调用startActivityForResult()方法。
To start a service:启动一个服务
A
a component that performs operations in the background without a user interface.
服务是运行在后台的没有用户界面的一个组件。
You can start a service to perform a one-time operation (such as download a file) by passing an
你可以启动一个服务来执行一次操作(如下载文件),通过传递一个Intent来startservice()。
The
the service to start and carries any necessary data.
Intent(意图)描述service的启动和携带任何必要的数据。
If the service is designed with a client-server interface, you can bind to the service from another component by passing an
如果这个服务是被设计成客户端的服务器的接口,你可以从另外一个组件通过传递一个Intent来bindService()——绑定这个服务。
To deliver a broadcast:传送一个广播
A broadcast is a message that any app can receive.
广播是一个消息,任何应用都能够接收。
You can deliver a broadcast to other apps by passing an
or
你可以发送一个广播到其他的应用,通过传递一个Intent(意图),使用
or
那么我们就来看看这个Intent吧。
Intent封装Android应用程序需要启动某个组件的“意图”。Intent是应用程序组件之间通信的重要媒介。
Android的应用程序包含三种重要的组件:Activity,Service,BroadcastReceiver,都是依靠Intent来启动的,Intent还可用于与被启动的组件交换消息。我前面就写过Intent在Activity之间传递数据的文章。
http://blog.csdn.net/pangrongxian/article/details/50166835
Intent对象包含Component、Action、Category、Data、Type、Extra和Flag这7种属性。
为了演示,我们假设创建两个Activity,FirstActivity和SecondActivity。
1.Component(组件)目的组件
假设现在要从FirstActivity跳转到SecondActivity中,下面是代码:
//创建一个ComponentName对象
ComponentName comp = new ComponentName(FirstActivity.this , SecondActivity.class);
Intent intent = new Intent();
//为Intent设置Component属性
Intent.setComponent(comp);
startActivity();
上面程序的代码创建ComponentName对象,并将该对象设置成Intent对象的Component属性,这样应用程序即可根据该Intent的“意图”去启动指定的组件了。(这个例子指定的组件是:SecondActivity)
上面的代码可以简写成:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
指定Component属性的Intent已经明确了它将要启动那个组件,因此这种Intent也被称为显示Intent,没有指定Component属性的Intent被称为隐式Intent——隐式Intent没有明确指定要启动那个组件,应用程序会根据Intent指定的规则去启动符合条件的组件,那些是符合条件的组件呢?下面说道到的Action属性就是了。
2.Action(动作):用来表示意图的动作
3.Category(类别):用于表示动作的类别
Intent的Action、Category属性都是一个普通的字符串而已,Action代表该Intent所要完成的一个抽象的动作,而Category则作为Action增加额外的附加条件。
Action属性和Category属性在哪里写呢?它们两个都是在Manifest.xml清单文件中<activity>中的intent-filter中写的:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<strong><action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在Intent类中定义了一批Action(动作),例如:
action常量 对应字符串 说明
ACTION_MAIN android.intent.action.MAIN ——应用程序入口
ACTION_VIEW android.intent.action.VIEW ——查看指定数据
ACTION_CALL android.intent.action.CALL ——直接向指定用户打电话
ACTION_SEND android.intent.action.SEND ——向其他人发送数据
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属性,通过action属性来查找组件SecondActivity。
用Intent的隐式意图跳转到另一个Activity
action字符串的是可以由你任意来写的,一般把action字符串的写成:包名.action.MY_ACTION
在FirstActivity中写如下代码:
Intent intent = new Intent();
setAction(包名.action.MY_ACTION);
startActivity(intent);
然后在Manifest.xml清单文件中SecondActivity的<activity>中的<intent-filter>中配置action属性
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<strong><action android:name="包名.action.MY_ACTION" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
只要FirstActivity中setAction()方法中,括号中参数(即action属性的字符串)对应上Manifest.xml清单文件中SecondActivity的<activity>中的<intent-filter>中action属性的字符串,FirstActivity就能够通过action属性找到SecondActivity,然后通过Intent跳转到SecondActivity。
这种通过action属性去启动组件的方式称为隐式Intent(没有指定Component属性的Intent被称为隐式Intent)
注意:在<intent-filter>中,必须添加category 属性才行,category.LAUNCHER是默认的category 。
4.Data(数据):表示与动作要操作的数据
5.Type(数据类型):对于data数据范例的描述
6.Extra(扩展信息)
7.Flag(标志位)
其他的几个属性不是很常用,用到的时候再查资料就可以啦!
这里也有视频讲解这些属性的用法的,给一个扣丁学堂的连接:
http://www.codingke.com/course/167/learn#lesson/455
这篇就讲些纯理论的东西吧,我自己也不太喜欢太过理论的东西,还是比较喜欢实战。
最后来看看Intent 的API文档的简介:(我只是顺便学习一下英文的o( ̄ヘ ̄o#))
An
Intentis
a messaging object you can use to request an action from another app
component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases:
一个Intent(意图)是一个消息对象,它通常能让你从另一个应用程序组件中请求一个动作。尽管Intents在好几个方面使应用程序组件之间的交流变得容易。这里有三个基本的使用例子:
To start an activity:启动一个Activity:
An
Activityrepresents
a single screen in an app. You can start a new instance of an
Activityby
passing an
Intentto
startActivity().
一个Activity代表一个应用程序中的一个屏幕(一个界面)。你可以通过传递一个Intent来startactivity(),启动一个新的activity实例。
The
Intentdescribes
the activity to start and carries any necessary data.
Intent(意图)描述Activity的启动和携带任何必要的数据。
If you want to receive a result from the activity when it finishes, call
startActivityForResult().
当一个activity结束掉的时候,如果你想要接收到一个activity返回的结果,你需要调用startActivityForResult()方法。
To start a service:启动一个服务
A
Serviceis
a component that performs operations in the background without a user interface.
服务是运行在后台的没有用户界面的一个组件。
You can start a service to perform a one-time operation (such as download a file) by passing an
Intentto
startService().
你可以启动一个服务来执行一次操作(如下载文件),通过传递一个Intent来startservice()。
The
Intentdescribes
the service to start and carries any necessary data.
Intent(意图)描述service的启动和携带任何必要的数据。
If the service is designed with a client-server interface, you can bind to the service from another component by passing an
Intentto
bindService().
如果这个服务是被设计成客户端的服务器的接口,你可以从另外一个组件通过传递一个Intent来bindService()——绑定这个服务。
To deliver a broadcast:传送一个广播
A broadcast is a message that any app can receive.
广播是一个消息,任何应用都能够接收。
You can deliver a broadcast to other apps by passing an
Intentto
sendBroadcast(),
sendOrderedBroadcast(),
or
sendStickyBroadcast().
你可以发送一个广播到其他的应用,通过传递一个Intent(意图),使用
sendBroadcast(),
sendOrderedBroadcast(),
or
sendStickyBroadcast().方法。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件