【原创】Android之应用程序的组成部分

        Android架构里定义了4种应用程序构件,用户的Android应用程序都是由这4种构件组成,他们分别是Activity(活动)、Service(服务)、BroadcastReceiver(广播接收器)和ContentProvider(内容提供器)。下面我们分别来看看他们都是做什么的。

1 Activity(活动)

        首先我们来看看官方的解释。

    • An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class.

        Android系统中的活动和桌面操作系统中独立的应用程序类似。活动都是有一些可执行代码组成的,活动可以由用户或操作系统在需要的时候启动。活动可以和用户进行交互,还可以通过查询(queries)和意图(Intent)向其他活动(Activity)或服务(Service)获取数据和服务。用户编写的大部分可执行代码是以活动的形式存在的。活动通常要负责屏幕的显示:每一个活动都有一个显示界面。如果一个活动不再处于活动状态,操作系统可以通过杀死它而提高手机内存的使用效率。

        一个应用就像一个传统浏览器一样,每个Activity就像浏览器中打开的页面一样。在传统浏览器中,任何时刻我们只能浏览一个页面,Android应用也是一样。在我们使用Android应用,我们只能看到当前的Activity。我们会在后面的文章中详细讲解Activity的相关知识。如有需求,先参看《Activities在Android平台的完整生态系统》。一个Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务,例如拨号、拍照、发送email、看地图。每一个activity被给予一个窗口,在上面可以绘制用户接口。窗口通常充满屏幕,但也可以小于屏幕而浮于其它窗口之上。

        一个应用程序通常由多个activities组成,他们通常是松耦合关系。通常,应用程序中有一个activity会被指定为"main"activity,即当第一次启动应用程序的时候呈现给用户的那个activity。一个activity可以启动另一个 activity,用以完成不同的动作。一个activity启动,前一个activity就停止了,但是系统会保留activity在一个栈上(“back stack”)。当一个新activity启动,它被推送到栈顶,取得用户焦点。Back Stack符合简单“后进先出”的原则,所以,当用户完成当前activity然后点击back按钮,它被弹出栈(并且被摧毁),然后之前的 activity恢复。

2 Service(服务)

        官方解释:

    • A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class.
    • It's possible to connect to (bind to) an ongoing service (and start the service if it's not already running). While connected, you can communicate with the service through an interface that the service exposes. For the music service, this interface might allow users to pause, rewind, stop, and restart the playback.
    • Like activities and the other components, services run in the main thread of the application process. So that they won't block other components or the user interface, they often spawn another thread for time-consuming tasks (like music playback). See Processes and Threads, later.

        Android系统中的服务与桌面系统或者服务器中的后台程序有些类似。服务同样是有一些可执行代码构成,它们启动后一直在后台运行,直到手持设备关机为止。服务通常不会显示用户界面。像Activity和其他组件一样,服务运行在应用进程中的主线程中,所以服务不会阻塞其他组件或用户接口。

        服务是运行在后台的一段代码。它可以运行在它自己的进程,也可以运行在其他应用程序进程的上下文(context)里面,这取决于自身的需要。其它的组件可以绑定到一个服务(Service)上面,通过远程过程调用(RPC)来调用这个方法。例如媒体播放器的服务,当用户退出媒体选择用户界面,仍然希望音乐依然可以继续播放,这时就是由服务 (service)来保证当用户界面关闭时音乐继续播放的。它跟Activity的级别差不多,但是他不能自己运行,需要通过某一个Activity或者其他Context对象来调用, Context.startService() 和Context.bindService()。两种启动Service的方式有所不同。这里要说明一下的是如果你在Service的onCreate或者onStart做一些很耗时间的事情,最好在Service里启动一个线程来完成,因为Service是跑在主线程中,会影响到你的UI操作或者阻塞主线程中的其他事情。

3 BroadcastReceiver(广播接收器)

        官方解释:

    • A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code — for example, announcements that the timezone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference. Applications can also initiate broadcasts — for example, to let other applications know that some data has been downloaded to the device and is available for them to use.
    • An application can have any number of broadcast receivers to respond to any announcements it considers important. All receivers extend the BroadcastReceiver base class.
    • Broadcast receivers do not display a user interface. However, they may start an activity in response to the information they receive, or they may use the NotificationManager to alert the user. Notifications can get the user's attention in various ways — flashing the backlight, vibrating the device, playing a sound, and so on. They typically place a persistent icon in the status bar, which users can open to get the message.

        广播接收器用于处理应用程序发出的服务请求、系统级别的事件消息。这些事件消息可能是Android系统本身发出的(比如电池电量过低),也可能来自系统系统中正在运行的其他应用程序。一个活动或者服务通过实现意图(Intent)接收器来向其他应用程序提供服务,意图接收器就是一段可执行代码,负责显影其他活动或者服务发出的数据或服务请求。想要发送请求的活动首先需要创建一个意图对象,然后将这个意图对象提交给Android操作系统,最后由Android操作系统决定使用哪一个应用程序处理该意图。

4 ContentProvider(内容提供器)

        官方解释:

    • A content provider makes a specific set of the application's data available to other applications. The data can be stored in the file system, in an SQLite database, or in any other manner that makes sense. The content provider extends the ContentProvider base class to implement a standard set of methods that enable other applications to retrieve and store data of the type it controls. However, applications do not call these methods directly. Rather they use a ContentResolver object and call its methods instead. A ContentResolver can talk to any content provider; it cooperates with the provider to manage any interprocess communication that's involved.

        内容提供器用于向其他活动或者服务提供数据服务。内容提供器通过使用标准的URI(Uniform Resource Identifier,统一资源标识符)接口为其他应用程序提供数据服务。数据的请求者甚至不需要知道服务的提供者是谁。例如,某个应用程序发出一个查询联系人信息的请求,请求格式如下:

Content://contacts/people

        Android操作系统就会查询系统中注册了这种URI格式的内容提供器,并将请求发送给相应的应用程序(如果给程序没有启动,系统会自动将其启动)。如果有多个应用程序都注册了同样的URI,那么操作系统就会提示用户选择其中的一个。

 

        在一个Android应用程序中,可能不会同时用到所有的4种构件,但是对于一个优秀的应用程序,一定会积极使用系统提供的这些机制,而不是重新编写某些功能火影编码一些对其他应用程序的引用。通过URI和意图机制,Android为用户提供了一个非常灵活的操作环境。用户可以方便地添加、删除和替换应用程序,URI和意图机制的松散耦合使得各种程序可以和谐地一起工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值