【编程语言】Android--Application Components应用组件概述

来源:互联网 发布:ios编程呼起app 编辑:程序博客网 时间:2024/06/07 11:20

ApplicationComponents

Application components are the essential building blocksof an Android application. Each component is a different point through whichthe system can enter your application. Not all components are actual entrypoints for the user and some depend on each other, but each one exists as itsown entity and plays a specific role—each one is a unique building block thathelps define your application's overall behavior.

There are four different types of application components.Each type serves a distinct purpose and has a distinct lifecycle that defineshow the component is created and destroyed.

Here are the four types of application components:

应用组件是开发Android应用重要的模块。每一个组件都是一个不同的入口点,系统能够通过它们进入你的应用程序。不是所有的组件都是用户的入口点,有一些组件还需视情况而定,但是每一个组件是它自己的入口并扮演者一个特殊的角色——每一个应用都包含这些组件。

有四种不同类型的应用组件.每一种类型提供一个不同的功能,有不同的生命周期,在生命中期里面定义了组件的创建和消亡的全过程。

下面是四种不同的应用组件:

Activities

An activity representsa single screen with a user interface. For example, an email application mighthave one activity that shows a list of new emails, another activity to composean email, and another activity for reading emails. Although the activities worktogether to form a cohesive user experience in the email application, each oneis independent of the others. As such, a different application can start anyone of these activities (if the email application allows it). For example, acamera application can start the activity in the email application thatcomposes new mail, in order for the user to share a picture.

An activity is implemented as a subclass of Activity andyou can learn more about it in the Activities developer guide.

Activities

Activities 代表一个单独与用户交互的界面。比如,邮件程序可能就有这样的一个Activity——显示了一个新邮件的清单,另一个Activity是创建邮件,还有Activity是阅读邮件。虽然在邮件应用中Activities之间协同工作组成了一个有凝聚力的用户体验,但是每一个Activity都是独立于其它的Activities的。这样,一个不同的应用程序能够开启这些Activities中的任何一个(当然是在对方应用程序语序的前提下)。比如照相机程序能够开启邮件程序中的Acitivity来发送带有图片的邮件

Avtivity作为Activity的一个子类被完成了,可以再Activiey的开发向导中学习更多相关内容。

Services

A service isa component that runs in the background to perform long-running operations orto perform work for remote processes. A service does not provide a userinterface. For example, a service might play music in the background while theuser is in a different application, or it might fetch data over the networkwithout blocking user interaction with an activity. Another component, such asan activity, can start the service and let it run or bind to it in order tointeract with it.

A service is implemented as a subclass of Service andyou can learn more about it in the Services developerguide.           

Services

services 是一个在后台执行的组件来执行需要长时间运行的操作或者为远程的进程执行操作。service没有提供用户交互界面。比如,有一个服务是在后台播放音乐然而用户却在不同的应用程序当中。或者可能它会通过网络提取数据但是不影响用户与activity之间的操作。其它的组件,比如Activity能够开启service并让它运行或者为了与它交互而与之捆绑。((网页上面播放音乐)。

ServiceService的一个子类的实现。

Content providers

content provider manages a shared set of application data. You canstore the data in the file system, an SQLite database, on the web, or any otherpersistent storage location your application can access. Through the contentprovider, other applications can query or even modify the data (if the contentprovider allows it). For example, the Android system provides a contentprovider that manages the user's contact information. As such, any applicationwith the proper permissions can query part of the content provider (suchas ContactsContract.Data)to read and write information about a particular person.

Contentproviders are also useful for reading and writing data that is private to yourapplication and not shared. For example, the Note Pad sampleapplication uses a content provider to save notes.

A contentprovider is implemented as a subclass of ContentProvider andmust implement a standard set of APIs that enable other applications to performtransactions. For more information, see the ContentProviders developer guide.

Content provider

content provider 管理一个可供分享的应用数据集。可以在文件系统中、SQLite数据库里、网络上、或者任何能够长期进行存储的位置来存储数据(前提是这些位置你的应用程序有权限访问),通过content provider,其它的应用程序能够查询或者甚至修改数据(只要content provider允许),比如,Android系统提供一个content provider来管理用户的联系人信息。这样,任何应用程序只要拥有权限就能够查询content provider的一部分(比如ContactsContrsct.data)来进行读或者写一个联系人的信息。

content provider可以用来度或者写你的应用程序私有的数据,比如NotePad示例中应用使用content provider来保存日记信息。

content provider作为ContentProvider的一个子类被完成,content provider一定要完成一系列标准的API函数,这些函数允许其他的应用程序执行权限交易,更过信息,敬请参看Content Provider开发向导。

 

Broadcast receivers

broadcastreceiver is a componentthat responds to system-wide broadcast announcements. Many broadcasts originatefrom the system—for example, a broadcast announcing that the screen has turnedoff, the battery is low, or a picture was captured. Applications can also initiatebroadcasts—for example, to let other applications know that some data has beendownloaded to the device and is available for them to use. Although broadcastreceivers don't display a user interface, they may create a statusbar notificationto alert the user when a broadcast event occurs.More commonly, though, a broadcast receiver is just a "gateway" toother components and is intended to do a very minimal amount of work. Forinstance, it might initiate a service to perform some work based on the event.

A broadcast receiver is implemented as a subclass of BroadcastReceiver andeach broadcast is delivered as an Intent object.For more information, see the BroadcastReceiver class.

Broadcast receivers

broadcast receiver是这样一个组件,对整个系统的消息进行相应。许多broadcasts来自于系统——比如,一条这样的广播:屏幕马上就要关闭了,电池电量低,或者捕获了一张图片。应用程序也能够发起广播信息-比如,让其它的应用程序知道有一些数据已经被下载下来了他们可以使用了。虽然broadcastreceiver没有显示用户接口但是他们可能当消息来的时候创建了一个状态条提示来提醒用户。更普遍的,虽然broadcastreceiver只是一个通向其它组件的好像是只做了很少量的工作。比如它可能根据broadcast来发起一个服务执行一些工作。

broadcast receiverBroadcastReceiver的子类的实现,并且每一个broadcast都作为一个intent对象被交付。

A unique aspect of the Android systemdesign is that any application can start another application’s component. Forexample, if you want the user to capture a photo with the device camera,there's probably another application that does that and your application canuse it, instead of developing an activity to capture a photo yourself. Youdon't need to incorporate or even link to the code from the camera application.Instead, you can simply start the activity in the camera application thatcaptures a photo. When complete, the photo is even returned to your applicationso you can use it. To the user, it seems as if the camera is actually a part ofyour application.

Android系统的一个特殊设计就是任何应用在拥有相应权限的情况下都能够开启另一个应用的组件。比如,如果你想要用户用照相机捕获一张图片,可能有另一个程序实现了捕获这个功能,这个时候你就能够直接使用它,这样就省去了自己开发一个activity来照相的麻烦。你也不需要纳入或者链接照相机应用程序的代码。相反的,你可以简单地启动照相机中的activity来照相。当照完的时候照片甚至直接返回到了你自己的应用程序当中,这样照片你就能使用了。对于用户来说,就好像照相机真的是当前的这个应用程序的一部分。、

When the system starts a component, itstarts the process for that application (if it's not already running) andinstantiates the classes needed for the component. For example, if yourapplication starts the activity in the camera application that captures aphoto, that activity runs in the process that belongs to the cameraapplication, not in your application's process. Therefore, unlike applicationson most other systems, Android applications don't have a single entry point(there's no main() function, for example).

当系统启动一个组件的时候,是开启了拥有这个组件的那个应用程序的进程(假设那个应用程序没有运行)对相应的类进行了实例化的操作。比如,如果你的应用程序开启了照相机程序中的activity来捕获照片,那么在进程中运行的这个activity属于照相机程序,而不属于你的应用进程,因此,这不像大多数其它系统中的应用程序,Android程序没有单一入口点比如(C语言中的main());

Because thesystem runs each application in a separate process with file permissions thatrestrict access to other applications, your application cannot directlyactivate a component from another application. The Android system, however,can. So, to activate a component in another application, you must deliver amessage to the system that specifies your intent to start aparticular component. The system then activates the component for you.

由于系统在不同的进程中运行不同的应用程序,不允许被别的应用程序访问,你的应用程序就不能直接启动其它程序的组件。然而Android系统能够启动其它应用中的组件,你必须发系统一条消息选择你的intent启动一个特殊的组件,然后系统就会为你启动相关组件了。只是需要系统帮忙了!

 

原创粉丝点击