Intent进阶 和 Intent-filter 学习笔记
1,Intent的基础用法
Intent是android中各activity之间通信的一个很重要的类,一般我们是这么使用的
- //创建一个intent
- Intent intent = new Intent();
- //压值进intent中
- //intent是基于一种基于Map的数据结构
- //传我们的基本数据类型的可以之间key,value方式传值
- intent.putExtra("hello","Hello world");
- //但是,传一个对象的时要注意,该对象要实现序列化窗口才可以传值
- //putExtra(String name, Serializable value)
- Demo demo = new Demo();
- intent.putExtra("Demo",demo);
- //然后,我们把这个intent传到另外一个activity
- intent.setClass(xxx.this,zzz.class);
- startActivity(intent);
- //-------
- //目标activity
- //获取传过来的intent Intent intent = getIntent();
- //从Intent中获取值
- String hello = intent.getStringExtra("Demo");
- Demo demo = (Demo)intent.getSerializable("Demo");
以上代码就是最基础的Intent的用法
2,深入Intent的构造和Intent-filter的基础入门
首先我们看下Intent的构造方法
- Intent()
- //Create an empty intent.
- Intent(Intent o)
- //Copy constructor.
- Intent(String action)
- //Create an intent with a given action.
- Intent(String action, Uri uri)
- //Create an intent with a given action and for a given data url.
- Intent(Context packageContext, Class<?> cls)
- //Create an intent for a specific component.
- Intent(String action, Uri uri, Context packageContext, Class<?> cls)
- //Create an intent for a specific component with a specified action and data.
出这些构造方法中我们,可以看出,新建一个Intent其实可以设置很多东西,这里我说说componentName,action,category,data
componentName
componentName 直译的话就是组件的名字的意思,如果我们写的一个类当作成一个组件,那么这个componentName就是用来封装我们写的这些类的位置.
- //创建一个component的对象
- ComponentName componentName = new ComponentName(Context.this, xxx.class);
- Intent intent = new Intent();
- Intent.setComponent(componentName);
- startActivity(intent);
- //----------
- //在xxxactivity中
- ComponentName comp = getIntent().getComponent();
- String packageName = comp.getPackName();
- String className = comp.getClassName();
其实细心的同学可以发现,其实跟setClass(),没什么区别吗,而且,还比setClass()麻烦,其实,setClass()是封装了这个功能而已,最实现还是要用到这个类.
Action,category,data简单入门
我感觉,学intent 和 intent-filter把action,category,data搞清楚才算真正的掌握了intent的用法.
Action
如果学过Structs2的同学应该不会陌生,所谓的action就是发送一个特定的请求,然后,由一个符合这个请求的activity响应
官方文档中是这么说action的:
The action largely determines how the rest of the intent is structured — particularly the data and extras fields — much as a method name determines a set of arguments and a return value. For this reason, it's a good idea to use action names that are as specific as possible, and to couple them tightly to the other fields of the intent. In other words, instead of defining an action in isolation, define an entire protocol for the Intent objects your components can handle.
我简要说下我理解的大义,有错欢迎指出
action 在很大一部分程度上决定你的intent 是如何构建的,特别是还有data,和 额外的一些字段,还有更多的是如何决定方法名,设置数组和返回的参数.由于这个原因,这是一个很好的方法去尽可能的具体的使用action,用action把这些字段紧密的联系在一起.另外,灵活定义你的action,然后把它定义在一个文档中为你的intent对象的组建能够正常处理
渣翻译…可能比谷歌翻译还有烂…有错欢迎指出!!!!!!!!!!!!!
category
要使一个action能够正常运行,category是必不可少的,关于category详细介绍请看开发者文档中的intent and intent-filter
这里说下注意的细节和使用.
首先,这句话是很重要的..取之于官方文档
Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT
" in their intent filters.
这句话,告诉我们在intent-filter(可以用xml创建也可以用代码创建,这里主要讲xml的创建),必须添加一个android.intent.category.DEFAULT
,至于,不添加会发生什么事…我花了半个小时的排错告诉我是无法运行的
- //在我们要运行的activity中添加
- <activity android:name=".SecondActivity" android:label="second"> <intent-filter >
- <action android:name="kg.tom.TEST"/>
- <category android:name="
- android.intent.category.DEFAULT " />
- </intent-filter>
- </activity>
- //firstActivity
- //在创建好的一个监听器里面方法中写上
- Intent intent = new Intent();
- intent.setAction("kg.tom.TEST");
- //利用action来运行相应的activity startActivity(intent);
注意:每个intent只能设置一个action,但是,可以设置多个category,这里的组合问题,自己好好想想吧.
data
因为就是一些参数的介绍,看官方文档就好了,我是这样理解这个DATA,例如,我们一个Mp3播放器,当我们在任务管理器中点击一个MP3文件,MP3这个文件就是一个data,就会触发我们,在intent-filter匹配这个Mp3,<data />的activity运行..
本文转自 liam2199 博客,原文链接:http://blog.51cto.com/youxilua/772705 如需转载请自行联系原作者

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android--SurfaceView播放视频
SurfaceView 先来介绍一下大部分软件如何解析一段视频流。首先它需要先确定视频的格式,这个和解码相关,不同的格式视频编码不同,不是这里的重点。知道了视频的编码格式后,再通过编码格式进行解码,最后得到一帧一帧的图像,并把这些图像快速的显示在界面上,即为播放一段视频。SurfaceView在Android中就是完成这个功能的。 既然SurfaceView是配合MediaPlayer使用的,MediaPlayer也提供了相应的方法设置SurfaceView显示图片,只需要为MediaPlayer指定SurfaceView显示图像即可。它的完整签名如下: void setDisplay(SurfaceHoldersh) 它需要传递一个SurfaceHolder对象,SurfaceHolder可以理解为SurfaceView装载需要显示的一帧帧图像的容器,它可以通过SurfaceHolder.getHolder()方法获得。 使用MediaPlayer配合SurfaceView播放视频的步骤与播放使用MediaPlayer播放MP3大体一致,只需要额外设置显示的SurfaceView即可...
- 下一篇
Android精选完整源码之控件指示器视频压缩日历源码!
Android框架+常用控件汇总-侧滑、上下拉加载、tab、各种ui 仿今日头条顶部导航指示器源码 android视频录制 视频压缩的源码 android滑动选择的尺子view源码 文件选择器 单片机和安卓应用,传感器 OpenglCamera android一款仿miui小米日历源码 android各种各样的跑马灯样式 Android高仿知乎的侧滑删除源码
相关文章
文章评论
共有0条评论来说两句吧...