Android四大组件及意图和意图过滤器

简介:




一、Android四大组件

1.Android四大组件 

    活动      Activity

    广播接收者   BroadcastReceiver

    服务      Service

    内容提供者   ContentProvider


2.Android四大组件的共性

  1)定义:都是继承相应的组件的类。

  2)清单文件配置:养成一个良好的习惯,创建一个组件类之后,立即在清单文件中的application结

   点下配置,并添加name属性,属性值为类的带包全名。

    Activity组件      配置<activity>标签

     BroadcastReceiver    配置<receiver>标签

    Service        配置<service>标签

    ContentProvider    配置<>标签


3.Android四大组件的区别 

  1)功能不同

  2)生命周期不同


二、意图

1.意图(Intent)

  • 意图的介绍    

    我觉得我没有必要把这个名词的概念说一遍了,因为官方文档说得十分地详细:

   An intent is an abstract description of an operation to be performed.  It can be used

 with startActivity to launch an ActivitybroadcastIntent to send it to any interested 

 BroadcastReceiver components, and startService(Intent) or bindService(Intent, 

 ServiceConnection, int) to communicate with a background Service.   

   它的意思大概是这样的: Intent是对将要执行的操作的一种抽象的描述,它可以用来开启一

 个Activity,或者发送它给任何感兴趣的广播接收者BroadcastReceiver组件,还可以与后台的服务

 Service交流。当然,它还可以跨应用交流信息。

   意图的作用:启动组件传递数据(putExtra与getXxxExtra方法)。  

   可见,意图与Android除了ContentProvider组件其它的组件都有关系,至于是什么关系,后面的博

 文中我会一一详述。

  • 意图的分类

     意图分为两类:显示意图隐式意图

    •  显示意图:通过类名来直接创建的意图叫做显示意图,常见的构造形式有如下几种:

       凡是通过.class来构造的Intent都是显示意图,常见有2种方式

 ComponentName:组件描述对象

      常用构造形式:ComponentName(应用包名, 完整类名)

      intent.setComponent(cn);    

     

 直接 new Intent(上下文,类字节码);        

wKiom1W0VYmS12b8AAFuR3hu5Bo646.jpgwKioL1W0V42BFLwpAABPw3oJ_Nw093.jpg

  显示意图一般用于启动当前应用中的活动Activity,服务Service,或向当前应用中广播接收者BroadcastReceiver发送意图。由于使用类名,目标唯一,所以安全性高。目标不需要在清单里配置意图过滤器。

 隐式意图:隐式意图一般用空参构造函数来构造实例,然后通过一些setXxx方法来设置与启         动目标意图过滤器相匹配的信息。  隐式意图一般用于启动其他应用中的中的活动Activity,服务Service,或向其它应用中广播接收者BroadcastReceiver发送意图。如启动系统自带的一些应用。

  由于可能匹配不成功或者有多个匹配结果,所以安全性差。目标需要在清单里配置意图过滤器。

  当然,隐式意图也可以用来启动本应用中的目标组件,只要它在清单里配置过意图过滤器。  


     :如果在非Activity(如Service)里利用意图启动一个activity,一定要为意图添加一个

           标记intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),要为activity创建一个栈环境

      

     :开启一个activity用startActivity,开启一个服务用startService。       

    

2.意图过滤器(IntentFilter)   

  • 介绍:       

     看官方文档:

    Structured description of Intent values to be matched.  An IntentFilter can        match against actions, categories, and data (either via its type, scheme, and/or path)

   in an Intent.It also includes a "priority" value which is used to order multiple 

   matching filters.

    IntentFilter objects are often created in XML as part of a package's 

   AndroidManifest.xml file, using intent-filter tags.   

     它的意思大概是这样的: 对要配置的的意图的值的描述,它可以通过acion、category、data

   来匹配一个意图。而且意图过滤器还有一个叫作“优先级”值,用来对多个配置的意图过滤器进行

   排序。

    通常意图过滤器是在清单文件中用intent-filter标签进行配置的,而<intent-filter>需要定

   义三个组件结点中。

     意图过滤器的作用:匹配意图       

  • 意图过滤器匹配规则     

    Filter Rules

    A match is based on the following rules.  Note that for an IntentFilter to match an Intent, three conditions must hold: the action and category must match, and the data (both the data type and data scheme+authority+path if specified) must match.   



  Action matches if any of the given values match the Intent action; if the filter

   specifies no actions, then it will only match Intents that do not contain an action. 

   (只要过滤器里至少有1个action与Intent对象匹配即可)  

  】】action表示动作,就是想要做的事情的名称。  


  Categories match if all of the categories in the Intent match categories given in the 

  filter.  Extra categories in the filter that are not in the Intent will not cause the 

  match to fail.  Note that unlike the action, an IntentFilter with no categories will     only match an Intent that does not have any categories.  

  (过滤器里的所有category必需与Intent里的所有category一一匹配)

  】】最常用的取值就2种情况

        <category android:name="android.intent.category.LAUNCHER" />   启动界面需

    要配置

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

        

  Data Scheme matches if any of the given values match the Intent data's scheme. The 

  Intent scheme is determined by calling getData() and getScheme() on that URI. Note that

  scheme matching here is case sensitive, unlike formal RFC schemes!  You should thus 

  always use lower case letters for your schemes.      

  (比如排打电话时创建的Intent对象,需要设置setData("tel:" + 电话号码),这个"tel:"就是data

   scheme)

  】】最常用的取值package、file、tel



  Data Type matches if any of the given values match the Intent type.  The Intent type 

   is determined by calling resolveType(ContentResolver).  A wildcard can be used for 

   the MIME sub-type, in both the Intent and IntentFilter, so that the type "audio/*" 

   will match "audio/mpeg", "audio/aiff", "audio/*", etc. Note that MIME type matching 

   here is case sensitive, unlike formal RFC MIME types!  You should thus always use 

   lower case letters for your MIME types.   


Intent拓展功能1:android拍照,调用系统相册,相片上传

   http://my.oschina.net/lhjtianji/blog/67006    

         一站式解决,Android 拍照 图库的各种问题.



补充:欲知详情,请查看Android官方文档。

   wKiom1W0T3Xi8vPeAALSDrfei_k793.jpg      




      本文转自屠夫章哥  51CTO博客,原文链接:http://blog.51cto.com/4259297/1678402,如需转载请自行联系原作者





相关文章
|
2月前
|
设计模式 Android开发
[Android 四大组件] --- BroadcastReceiver
[Android 四大组件] --- BroadcastReceiver
33 0
|
3月前
|
Android开发 开发者
什么是Android Jetpack,它包括哪些组件?
什么是Android Jetpack,它包括哪些组件?
39 0
|
4月前
|
数据库 Android开发
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
47 0
|
4月前
|
XML Java Android开发
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
72 0
|
2月前
|
数据可视化 Android开发
[Android 四大组件] --- Service
[Android 四大组件] --- Service
24 0
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
2月前
|
存储 数据库 Android开发
安卓四大组件是什么?
安卓四大组件是什么?
|
3月前
|
数据库 Android开发 开发者
Android基础知识:什么是Android应用的四大组件?
Android基础知识:什么是Android应用的四大组件?
59 1
|
4月前
|
XML 安全 Java
Android Studio App开发之广播组件Broadcast的讲解及实战(包括收发标准、有序、静态广播实现手机震动功能 附源码)
Android Studio App开发之广播组件Broadcast的讲解及实战(包括收发标准、有序、静态广播实现手机震动功能 附源码)
35 0
|
6月前
|
设计模式 网络协议 Java
《移动互联网技术》 第十章 系统与通信: 掌握Android系统的分层架构设计思想和基于组件的设计模式
《移动互联网技术》 第十章 系统与通信: 掌握Android系统的分层架构设计思想和基于组件的设计模式
63 0