应用程序基础Android Developers外文翻译资料

 2022-12-08 15:49:35

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; its the file users download to their devices. All the code in a single .apk file is considered to be one application.

In many ways, each Android application lives in its own world:

1. By default, every application runs in its own Linux process. Android starts the process when any of the applications code needs to be executed, and shuts down the process when its no longer needed and system resources are required by other applications.

2. Each process has its own virtual machine (VM), so application code runs in isolation from the code of all other applications.

3. By default, each application is assigned a unique Linux user ID. Permissions are set so that the applications files are visible only to that user and only to the application itself — although there are ways to export them to other applications as well.

Its possible to arrange for two applications to share the same user ID, in which case they will be able to see each others files. To conserve system resources, applications with the same ID can also arrange to run in the same Linux process, sharing the same VM.

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

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 dont have a single entry point for everything in the application (no main() function, for example). Rather, they have essential components that the system can instantiate and run as needed. There are four types of components:


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.

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

A view hierarchy is placed within an activitys 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.)


A service doesnt 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.

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


应用程序基础Android Developers



1 默认情况下,每个应用程序均运行于它自己的Linux进程中。当应用程序中的任何代码需要被执行时,Android启动此进程,而当不再需要此进程并且其它应用程序又请求系统资源时,则关闭这个进程。

 2 每个进程都有其独有的虚拟机(VM),所以应用程序代码与所有其它应用程序代码是隔离运行的。

3 默认情况下,每个应用程序均被赋予一个唯一的Linux用户ID,并加以权限设置,使得应用程序的文件仅对此用户及此应用程序可见——尽管也有其它的方法使得这些文件同样能为其他应用程序所访问。

1 应用程序组件



1 Activity




窗口显示的可视内容是由一系列层次化view构成的,这些view均继承自 View 基类。每个view均控制着窗口中一块特定的矩形区域。父级view包含并组织其子view的布局。叶节点view(位于层次结构最底端)在它们控制的矩形区域中进行绘制,并对用户直达其区域的操作做出响应。因此,view是activity与用户进行交互的界面。例如,view可以显示一个小图片,并在用户指点它的时候产生动作。Android有一些预置的view供开发者使用——包括按钮、文本域、滚动条、菜单项、复选框等等。

view层次结构是由Activity.setContentView() 方法放入activity的窗口之中的。content view是位于层次结构根位置的View对象。(参见独立的用户界面文档以获取关于view及层次结构的更多信息。)

2 Service





3 Broadcast receiver

broadcast receiver是一个与注于接收广播通知信息,并做出相应处理的组件。许多广播是由系统代码产生的——例如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以发起广播——例如,通知其它应用程序一些数据已经下载到设备上并处于可用状态。

一个应用程序可以拥有任意数量的broadcast receiver,以对所有它认为重要的通知信息予以响应。所有的receiver均继承自BroadcastReceiver基类。

broadcast receiver没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者也可以使用NotificationManager来通知用户。通知可以用多种方式来吸引用户的注意力──闪动背光灯、震动设备、播放声音等等。通知一般是在状态栏上放一个持丽的图标,用户可以打开它并获取消息。

4 Content provider

content provider将一些特定的应用程序数据供给其它应用程序使用。数据可以存储于文件系统、SQLite数据库或其它有意丿的方式。content provider继承于ContentProvider 基类,实现了一套使得其他应用程序能够检索和存储它所管理的类型数据的标准方法。然而,应用程序并不直接调用返些方法,而是使用一个 ContentResolver 对象,调用它的方法作为替代。ContentResolver可以与任何content provider进行会话;与其合作对任何相关的进程间通讯进行管理。

参阅独立的Content Providers文档以获得更多关于使用content provider的信息。



当接收到ContentResolver发出的请求后,content provider被激活。而其它三种组件——activity、service和broadcast receiver,被一种叫做intent的异步消息所激活。intent是一个保存着消息内容的Intent对象。对于activity和service来说,它指明了所请求的操作名称,并指定了用来操作的数据的URI和其它一些信息。例如,它可以承载一个对一个activity的请求,让它为用户显示一张图片,或者让用户编辑一些文本。而对于broadcast receiver来说,Intent对象指明了所通报的操作。例如,它可以对所有感兴趣的对象通报照相按钮被按下。


 1 通过传递一IntentContext.startActivity()Activity.startActivityForResult(以启动(或指定新工作给)一个activity。相应的activity可以通过调用自身的 getIntent() 方法来查看最刜激活它的intent。Android通过调用activity的onNewIntent()方法来传递给它随后的任何intent。

一个activity经常启动另一个activity。如果它期望它所启动的那个activity迒回一个结果,它会调用startActivityForResult()而不是startActivity()。例如,如果它启动了另外一个activity以使用户挑选一张照片,它也许想知道哪张照片被选中了。其结果将会被封装在一个Intent对象中,并传递给发出调用的activity的onActivityResult() 方法。

2 通过传递一个Intent对象至Context.startService()以启动一个service(或向正在运行的service给出一个新的指令)。Android调用此service的 onStart()方法并将Intent对象传递给它。

与此类似,一个intent可以被传递给 Context.bindService()以建立一个处于调用组件和目标service乀间的活动连接。此service会通过onBind() 方法的调用来获取此Intent对象(如果此service尚未运行,bindService()会先启动它)。例如,一个activity可以建立一个不前述的音乐回放service的连接,这样它就可以提供给用户一些途径(用户界面)来控制回放。这个activity可以调用 bindService()来建立此连接,然后调用service中定之的方法来控制回放。


 3 应用程序可以通过传递一个Intent对象至 Context.sendBroadcast() ,Context. sendOrderedBroadcast(), 以及Context.sendStickyBroadcast()和其它类似方法来发起一个广播。Android会调用所有对此广播有兴趣的broadcast receiver的 onReceive()方法,将此intent传递给它们。

1.2 关闭组件

content provider仅在响应来自ContentResolver的请求时处于活动状态。而broadcast receiver仅在响应一条广播信息的时候处于活动状态。所以没有必要去显式地关闭返些组件。


activity可以通过调用自身的finish()方法来关闭。一个activity可以通过调用finishActivity()方法来关闭另外一个activity(它用startActivityForResult() 启动的)。

 service可以通过调用自身的stopSelf()方法,或调用 Context.stopService()来停止。


1.3 manifest文件




lt;activitygt;元素的name属性指定了实现此activity的 Activity子类。icon和label属性指向包含展示给用户的此activity的图标和标签的资源文件。

其它组件也以类似的方法声明——lt;servicegt; 元素用于声明service, lt;receivergt; 元素用于声明broadcast receiver,而 lt;providergt; 元素用于声明content provider。未在manifest文件中进行声明的activity、service以及content provider将不为系统所见,从而也就永不会被运行。然而



您需要先支付 30元 才能查看全部内容!立即支付