Android Application Fundamentals——Android应用程序基础知识

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Application Fundamentals——Android应用程序基础知识相关的知识,希望对你有一定的参考价值。

Application Fundamentals——应用程序基础知识


android applications are written in the Java programming language. The compiled Java code — along with any data and resource files required by the application — is bundled by the aapt tool into an Android package, an archive file marked by an .apk suffix. This file is the vehicle for distributing the application and installing it on mobile devices; it‘s the file users download to their devices. All the code in a single .apk file is considered to be one application.
Android应用程序是用Java编程语言编写的。编译好的Java代码——随同全部应用程序要使用的数据和资源文件一起——使用aapt tool打包成一个Android包,即一个后缀为.apk的归档文件。此文件是公布应用程序和在移动设备上安装应用程序的有媒介;它是用户下载到他们设备上的文件。在一个.apk文件里的全部代码被觉得是一个应用程序。

In many ways, each Android application lives in its own world:
从非常多方面来讲。每个Android程序都执行在自己的空间里:

  • By default, every application runs in its own Linux process. Android starts the process when any of the application‘s code needs to be executed, and shuts down the process when it‘s no longer needed and system resources are required by other applications.     默认的,每个程序执行在其自己的Linux进程中。

    Android在应用程序中某段代码须要被执行时启动一个进程,不再使用或系统资源被其它应用程序请求时关闭这个进程。

  • Each process has its own Java virtual machine (VM), so application code runs in isolation from the code of all other applications.     每个进程都有自己的Java虚拟机(VM),一个应用程序代码执行时同其它程序代码分开,不受不论什么其它程序的代码的影响。


  • By default, each application is assigned a unique Linux user ID. Permissions are set so that the application‘s files are visible only that user, only to the application itself — although there are ways to export them to other applications as well.     默认的。每个应用程序享有一个独自的Linux用户ID。由于权限设定原因。一个应用程序的文件仅仅有本用户(其自己)可见——当然,也有把他们导出给其它应用程序的机制。

It‘s possible to arrange for two applications to share the same user ID, in which case they will be able to see each other‘s files. To conserve system resources, applications with the same ID can also arrange to run in the same Linux process, sharing the same VM.
能够为两个应用程序安排为使用同一个用户ID,这样,他们的文件相互可见。为为了节省系统资源。拥有同样ID的程序能够作为一个Linux进程执行,分享同一个虚拟机。


Application Components——应用程序组件

A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images and another application has developed a suitable scroller and made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesn‘t incorporate the code of the other application or link to it. Rather, it simply starts up that piece of the other application when the need arises.
Android的一个中心功能就是应用程序能够使用其它应用程序的元素(在提供元素的应用程序同意的情况下)。比如,假设您的应用程序想要显示一系列带有滑屏效果功能的图片,然后某个应用程序正巧开发出了合适的滑屏的模块。并且同意共享。您就能够调用哪个滑屏模块处理这些图片并显示出来。而不是您自己再去开发一个。您的应用程序并没有包括或链接到了其它的应用程序的代码。可是当请求发出后,您的应用程序确实能够简单的使用其它程序的部分功能。

For this to work, the system must be able to start an application process when any part of it is needed, and instantiate the Java objects for that part. Therefore, unlike applications on most other systems, Android applications don‘t have a single entry point for everything in the application (no main() function, for example). Rather, they have essentialcomponents that the system can instantiate and run as needed. There are four types of components:
为了达成这个过程。系统必须能够再应用程序的某个部分被请求时启动这个程序的进程。然后为那个部分的Java对象创建实例。

因此不像其它操作系统上的应用程序那样,Android程序没有一个单一的应用程序入口(比如没有main()函数)。然而他们含有当系统须要时创建实例的实质的组件。

组件有四种:

Activities——活动
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.
活动为须要用户关注和确认的部分显示一个可见的用户界面。

比如。活动会显示一个用户可选的菜单项的列表。或是显示带有标签的照片。一个文本信息应用程序可能有一个显示联系人列表来发送信息。又有一个为选定的联系人撰写信息的活动,还有其它回想旧信息或改变设定的活动。

尽管他们总体组成一个相互关联的用户界面,可是每个活动都是相对独立的。每个都作为Activity基类的一个子类来实现。


An application might consist of just one activity or, like the text messaging application just mentioned, it may contain several. What the activities are, and how many there are depends, of course, on the application and its design. Typically, one of the activities is marked as the first one that should be presented to the user when the application is launched. Moving from one activity to another is accomplished by having the current activity start the next one.

一个应用程序可能仅仅含有一个活动,或者像上边提到的文本信息程序一样含有几个。

有几个活动,这些活动是什么样的取决于您的应用程序是怎样设计的。

最典型的是将一个活动设计为第一个活动,应用程序被载入时呈现给用户。从一个活动转到还有一个是通过在当前活动開始还有一个来实现的。


Each activity is given a default window to draw in. Typically, the window fills the screen, but it might be smaller than the screen and float on top of other windows. An activity can also make use of additional windows — for example, a pop-up dialog that calls for a user response in the midst of the activity, or a window that presents users with vital information when they select a particular item on-screen.

每个活动提供了一个用以绘制的默认窗体。典型的,窗体填满整个屏幕,可是它覆盖在其它窗体上时,会变得略小一点。一个活动能够使用多个窗体——比如,在窗体中央显示一个须要用户回应的弹出窗体,或者在用户选择屏幕上一个特定项目是时为他显示重要信息的窗体。


The visual content of the window is provided by a hierarchy of views — objects derived from the base View class. Each view controls a particular rectangular space within the window. Parent views contain and organize the layout of their children. Leaf views (those at the bottom of the hierarchy) draw in the rectangles they control and respond to user actions directed at that space. Thus, views are where the activity‘s interaction with the user takes place. For example, a view might display a small image and initiate an action when the user taps that image. Android has a number of ready-made views that you can use — including buttons, text fields, scroll bars, menu items, check boxes, and more.

窗体的可见内容由一组view层提供——从View基类衍生出来的对象。每个view控制窗体中的一块矩形区域。父view包括并组织子view的布局。

页view(底层的view)绘制它们管理的矩形,并响应在“空白”的地方的动作。就是说。view就是活动和用户互动的地方。比如,一个view显示一个小图片,然后初始化用户点击这个小图片后的动作。

Android有非常多已做好的view供您选择——包括按钮,文本输入框。滚动栏,菜单项。多选列表等。


A view hierarchy is placed within an activity‘s window by the Activity.setContentView() method. The content view is the View object at the root of the hierarchy. (See the separate User Interface document for more information on views and the hierarchy.)

通过使用Activity.setContentView()方法将一组view层放置在一个活动的窗体中。

content view是在层的最底部的View对象。(參见独立User Interface的文档获取很多其它有关view和层的信息。)


Services——服务
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.
服务没有可见的用户界面,可是能够在后台执行随意长的时间。比如,一个服务能够在用户转向其它工作后仍在后台播放音乐,或从网上下载数据,或计算什么然后在活动须要的时候提供给活动。服务是从Service基类继承来的。


A prime example is a media player playing songs from a play list. The player application would probably have one or more activities that allow the user to choose songs and start playing them. However, the music playback itself would not be handled by an activity because users will expect the music to keep playing even after they leave the player and begin something different. To keep the music going, the media player activity could start a service to run in the background. The system would then keep the music playback service running even after the activity that started it leaves the screen.

一个基本的样例就是一个从列表中播放音乐的媒体播放器。播放器程序可能会有一个或几个同意用户选择希望播放的音乐然后显示播放的活动。

可是音乐的回放过程本身不会使用一个活动由于用户希望在切出播放器界面做别的事时音乐也能一直放下去。为了保持播放继续,活动能够启动一个服务在后台执行。

然后即使启动这个服务的活动退出,音乐播放服务也能继续执行。

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.

像活动和其它组件一样,服务执行在程序进程的主线程中。

这样它们就不会阻碍其它组件或是用户界面,它们一般是执行一个子线程来进行实时的任务(比如音乐回放)。

过后请參见Processes and Threads


Broadcast receivers——广播接受器
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.
broadcast receiver仅仅是接受并对广播信息做出反应。

多数的广播是由系统代码发出的——比如,时区变化的通知、电量低的通知、照了一张照了一张照片 或者用户改变了语言设定。应用程序也能够初始化一条广播——比如让其它应用程序知道有些数据下载完毕能够使用难过了。

An application can have any number of broadcast receivers to respond to any announcements it considers important. All receivers extend the BroadcastReceiver base class.

应用程序能够含有随意数量的广播接收器对随意其觉得重要的的广播进行响应。全部的广播接收器均由BroadcastReceiver基类衍生

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 BroadcastReceiver 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.

广播接收器不显示用户界面。可是它能够启动一个活动来对接收到的信息进行响应或者是使用一个BroadcastReceiver来通知警告用户。

Notification(通知)能够一各种方式引起用户注意——使背景灯闪烁。使设备振动,播放声音等等。

一般是在状态条上显示一个不会消失图标,用户能够拉下状态条获得通知。


Content providers——内容提供者
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 aContentResolver 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.
content provider 使程序中特定的数据可被其它程序使用。共享的数据能够存储在文件系统中、SQLite数据库中或其它不论什么可用的介质。内容提供者由ContentProvider基类派生。实现了一系列的   使其它程序获取和存储其支持的数据格式的   方法。

可是应用程序不会直接调用这些方法。而是使用ContentResolver对象,然后调用这个对象的方法。ContentResolver能与不论什么内容提供者对话,它与提供者合作来管理全部涉及到的进程间通信。

See the separate Content Providers document for more information on using content providers.

获取很多其它使用内容提供者的具体信息请參见单独的文档——Content Providers


Whenever there‘s a request that should be handled by a particular component, Android makes sure that the application process of the component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary.
当有应被特殊的组件操作的请求时,Android会确认这个组件是否正在执行,假设未在执行,则启动它。假设正在执行则检測须要的类是否实例化。如未实例化,则为类创建实例。


Activating components: intents——激活别的组件的组件:意图

Content providers are activated when they‘re targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.
内容提供者获得ContentResolver请求的指向后,会被激活。另外的三种组件——活动。服务,广播接收器——是靠异一种叫做intents(意图)的异步消息激活的。

意图是   承载了这样的异步消息内容的   Intent的对象。

对于活动和服务。它主要是为被请求的动作命名。然后指定操作此数据的URI。比如,它可能携带让一个活动为用户展现一张图片或者让用户编辑文本的请求。

对广播接收器,Intent对象为将要广播的内容命名。

比如。它可能会向有兴趣的接收器公布照相键被按下的广播。

There are separate methods for activiating each type of component:
激活不同的组件需使用不同的方法:

  • An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity‘s onNewIntent() method to pass it any subsequent intents.          向Context.startActivity()Activity.startActivityForResult()传递Intent对象能够激活(或获得新的任务)一个活动。响应的活动能够通过调用getIntent()方法查看导致自己被激活的最初的意图。Android调用活动的onNewIntent()方法来传递之后的意图。

    One activity often starts the next one. If it expects a result back from the activity it‘s starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that‘s passed to the calling activity‘s onActivityResult() method.        一个活动一般会开启还有一个活动。假设前一个活动希望从新启动的活动获得一个执行结果,能够调用startActivityForResult()方法来取代startActivity()方法。比如。假设一个老活动开启了一个让用户照相的新活动。又希望从新活动获得这张照片。

    新活动的执行结果是通过一个   传递给老活动的onActivityResult()方法的   Intent对象传递的。

  • A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service‘s onStart() method and passes it the Intent object.

    Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.        服务通过向Context.startService()传递一个Intent对象启动(或给予新任务)Android调用服务的onStart()方法然后将Intent对象传递给服务。相似的,一个意图能够被传给Context.bindService()来建立呼叫组件和被叫组件的实时连接。调用服务的onBind()来使其接受Intent对象。(假设服务未在执行。您能够选择使用bindService()来启动它。)比如上面提到过的音乐播放服务,活动会希望建立一个与此服务的连接以向用户提供控制播放的功能(比如一个UI界面)。

    这时活动就会调用bindService()来建立连接。然后调用服务中定义的方法来控制播放。

  • A later section, Remote procedure calls, has more details about binding to a service.          之后的章节会讲到Remote procedure calls,那有更具体的绑定服务的信息。


  • An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast()Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.          程序能够通过向相似   Context.sendBroadcast()Context.sendOrderedBroadcast()Context.sendStickyBroadcast()。及它们的变种   的方法传递一个Intent对象来初始化一条广播。


For more on intent messages, see the separate article, Intents and Intent Filters.
获得很多其它关于意图的信息,查看单独的章节Intents and Intent Filters


Shutting down components——关闭组件

A content provider is active only while it‘s responding to a request from a ContentResolver. And a broadcast receiver is active only while it‘s responding to a broadcast message. So there‘s no need to explicitly shut down these components.

内容提供者仅仅有在响应来自ContentResolver的请求时才会处于激活状态。广播接收器仅仅有在响应广播信息时才处于激活状态。所以没有必要显式地关闭这些组件。

Activities, on the other hand, provide the user interface. They‘re in a long-running conversation with the user and may remain active, even when idle, as long as the conversation continues. Similarly, services may also remain running for a long time. So Android has methods to shut down activities and services in an orderly way:

活动,相反的。提供了用户界面。他们会与用户长时间地对话,并且可能在整个对话过程,即使处于空暇状态,都保持执行状态。

同样的,服务也可能会执行非常长时间。因此。Android提供了下列的关闭活动和服务的方法:

  • An activity can be shut down by calling its finish() method. One activity can shut down another activity (one it started with startActivityForResult()) by calling finishActivity().

活动能够通过调用他的finish()方法进行关闭。

一个活动也能够调用finishActivity()关闭还有一个活动(但仅仅能是由他调用startActivityForResult()启动的)。

一个服务能够通过调用他的html#stopSelf%28%29" style="color: rgb(0, 102, 153); text

以上是关于Android Application Fundamentals——Android应用程序基础知识的主要内容,如果未能解决你的问题,请参考以下文章

FunDA(15)- 示范:任务并行运算 - user task parallel execution

FunDA:一个开源的函数式数据处理工具库,也是Slick的补充

FunDA- 流动数据行操作:FDAPipeLine operations using scalaz-stream-fs2

FunDA- Reactive Streams:Play with IterateesEnumerator and Enumeratees

FunDA(16)- 示范:整合并行运算 - total parallelism solution

FunDA- 数据流内容控制:Stream data element control