1 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#39;s 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 application#39;s code needs to be executed, and shuts down the process when it#39;s 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 application#39;s 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.
It#39;s possible to arrange for two applications to share the same user ID, in which case they will be able to see each other#39;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.
1.1 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#39;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.
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#39;t 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:
(1)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.
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#39;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.
A view hierarchy is placed within an activity#39;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.)
(2)Services
A service doesn#39;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.
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 th
全文共18365字,剩余内容已隐藏,支付完成后下载完整资料
1 应用基础
安卓应用程序是用Java变成语言来编写的。这些被编译的Java代码和应用程序所需要的资源和数据通过APT工具打包成一个Android包文件。这是一个档案文件,这个文件使用.apk作为后缀.这个文件是分配这个应用程序和把这个应用程序安装到移动设备的载体;这是一个用户需要下载到他们设备的文件。一个的.apk文件中的所有代码会被认为是一个应用程序。
从多个角度看,每个Android应用程序都存在于它自己的世界里。
- 从默认的情况来看,每个应用程序在它自己的Linux进程中运行。当有一个应用程序的代码需要被执行时,Android启动这个进程。当不再需要执行或者系统资源被其他的应用程序请求时,就关闭这个进程。
- 每个进程都有属于自己的虚拟机(VM),所以应用程序的代码的运行是独立于其他应用程序的代码的。
- 从默认的情况来看,每个应用程序都被赋予了一个唯一的Linux用户ID,并加以权限设置,所以这个应用程序的文件只对这个用户和这个应用程序它本身可见—尽管这里也有其他的方式去使得他们对其他的应用程序可见。
这里可以安排两个应用程序去共同使用同一个用户ID,在这种情况下,它们将能够看到对方的文件。为了保护系统资源,使用相同ID的程序可以被安排到同一个Linux进程中运行,同时使用同一个虚拟机。
应用程序组件
Android一个核心特性就是一个应用程序可以利用其它应用程序的元素(如果这些应用程序允许)。例如,如果你的应用程序需要去展示一个卷动的图片列表而另外一个应用已经开发了一个滚动条而且可以让其它的应用程序使用的话,你就可以调用这个滚动条去做这个工作,而不是去自己开发一个。你的应用程序并没有合并或连接其它应用程序的代码。只是在其它的应用程序需要被调用时启动。
为此,当任何部分需要时这个系统必须能够启动,而且将此部分的Java对象实例化。因此,不像其他大多数系统那样,Android应用程序没有为所有的应用程序提供一个单一的入口点(例如没有main()函数)么事为系统提供了使系统实例化和运行需要的组件。这里一共有四种组件类型:
(1)Activities
Activity为用户提供一个可视化的界面。例如,一个activity可以呈现一个菜单项目列表供用户选择,或者可以展示一些图片并配上它们的说明。一个手机短信应用可能有一个activity去显示要发送消息的联系人列表,第二个activity是用来选定联系人写短息的activity以及去回顾短信或者其他改变设置的activity。尽管它们在一个有结合力的用户界面上工作,但是其中的每个activity都独立于其他的activity,每一个都执行以Activity类为基类的子类。
一个应用程序可能只由一个activity组成,或者,像刚才提到的手机短信应用程序那样,由很多个组成。每个activity的作用和有多少个activity当然是取决于应用程序和它的设计。典型的,其中一个activity被标记为当这个程序启动后用户看到的第一个。用当前的activity启动下一个activity把一个activity转向另一个、
每个activity被给予一个默认的用来进行绘制的窗口。典型的是,这个窗口是覆盖全屏的,但是这个窗口也可能是比屏幕尺寸小一些而且浮动于其他的窗口之上的窗口。一个activity也可以使用额外的窗口——例如,一个在activity运行过程中弹出的供用户响应的对话框,这是一个当用户选择了屏幕上特定项目后显示的有必要信息的窗口。
这个窗口的视觉内容是由一系列的层次化的view构成的,这些对象都来源于View基类。每个view都控制着窗口中一个特别的矩形空间。父view包含并组织其子view的布局。也节点view(这些位于底层)在他们控制的矩形区域中进行绘制,并且对用户在此区域内直接的操作做出响应。因此,views是activity和用户交互的地方。
例如,当用户轻点一个图片时一个view可展示一个小图片产生一些初始的操作。Android有一些已经设置好的view给开发者使用——包括按钮、文本域、滚动条、菜单项、复选框等等。
View的层次是通过Activity.setContentView()方法被放置到activity的窗口中的,content view是位于层次结构的根部的view对象。(参考独立的用户界面文档,用来获得关于别玩和层次结构的更多信息。)
(2) Service
Service没有可视化用户界面,而是在不确定的一段时间内在后台运行。例如,一个service可能在用户做其他事情的时候在后台播放音乐或者从网上获取数据或者计算一些东西并把结果提供给需要这个的activity。每个service都集成了service基类。
一个很好的例子是一个媒体播放器从播放列表中播放歌曲。这个播放器应用可能有一个或多个activity来给用户选择歌曲并播放。然而,这个音乐播放的任务应该由activity来处理,因为用户期望这个音乐一直在播放即使他们离开播放器去做其他事情时。为了保持音乐持续播放,媒体播放器的activity可以启动一个service在后台运行 。系统将保持音乐播放服务的运行即使在这个activity不再显示于屏幕。
连接到(绑定)一个正在运行的service(如果service没有运行就启动service)。当连接以后,你可以通过这个service暴露出来的接口与这个service通信。对于这个音乐service,这个借口可以允许用户去暂停、回退、停止、和重新开始播放。
就像activities和其他的组件一样,service运行于这个应用程序进程中的主线程内。所以他们将不会阻碍其他的组件或者其他的用户界面,它们经常会派生另外的线程来执行一些时间消耗型任务。稍后参见进程和线程的介绍。
(3)Broadcastreceiver
Broadcast receiver是一个与专注接受广播通知信息,并作出相应处理的组件。许多广播都是有系统代码组成的——例如,声明时区的改变,电池电量低的状况,照片的拍摄或者用户选项的改变。应用程序也可以发起广播——例如,让其他的应用程序一些数据已经下载到设备上,并处于可用的状态。
一个应用程序可以有任意数量的广播接收者来作出重要的响应。所有的receiver都继承于BroadcastReceiver基类。
broadcast receiver没有给用户展示界面。然而他们会启动一个activity来响应他们收到的信息,或者他们也可以用NotificationManager来提醒用户。Notification可以用多种方式来得到用户的注意——背光的闪动、设备的震动、声音的播放等等。通知一般是在状态栏放置一个持久的图标,用户可以打开它来获取信息。
(4)Contentproviders
content provider使一些应用程序特定的数据可供给其他应用程序进行处理。这些数据可以存储于文件系统、SQLite数据库或者其他有意义的方式。content provider继承于Content Provider基类,实现了一套使得其他应用程序能够线索和存储它所管理的数据类型的标准方法。然而,应用程序不直接调用这些方法。而是他们用这个ContentResolver对象调用这些方法。一个ContentResolver可以与任何的content provider进行交流;进程间通讯的管理通过它与提供者的合作得到了解决。
参考独立的content providers文档来获得更多关于使用content providers的信息。
无论什么时候有一个请求需要被特定的组件处理时,Android会确保这个组件的应用进程处于运行状态。必要时会启动它,并确保那个组件的一个合适的实例可用,必要时会创建那个实例。
1.2激活组件:intent
当接收到contentresolver发出的请求后,content provider被激活。其他的三个组件——活动、服务和广播——被一种叫异步信息所激活。Intent是一个保存着消息内容的对象。对于活动和服务来说,它指明了所请求的操作名称,并指定了用来操作数据的URI和其他一些信息。例如,它可以表达一个对活动的请求,让它为用户显示一张图片或者让这个用户编辑一些文本。对于广播的接收对象,这个目的对象的名字被声明,例如,它可以对相关方进行声明当拍照按钮被按下。
对每种组件来说这里有一些不同的方法:
- 一个活动通过被启动(或者被给予新的事情)相应的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中定之的方法来控制回放。
稍后的远程方法调用一节有关于如何绑定至一个service的更多细节。
3应用程序可以通过传递一个Intent对象至 Context.sendBroadcast() ,Context. sendOrderedBroadcast(), 以及Context.sendStickyBroadcast()和其它类似方法来发起一个广播。Android会调用所有对此广播有兴趣的broadcast receiver的 onReceive()方法,将此intent传递给它们。
1.3 关闭组件
content provider仅在响应来自ContentResolver的请求时处于不同活动状态。而broadcast receiver仅在响应一条广播信息的时候处于各种活动状态。所以没有必要去显式地关闭这组件。
而activity则不同,它提供了用户界面。只要会话依然持续,无论会话过程有无空闲,activity同用户进行长时间会话且可能一直处于活动状态。与此相似,service也会在很长一段时间内在后台保持运行。所以Android为关闭activity和service提供了一系列有序的方法。
activity可以通过调用自身的finish()方法来关闭。一个activity可以通过调用finishActivity()方法来关闭另外一个activity(它用startActivityForResult() 启动的)。
service可以通过调用自身的stopSelf()方法,或调用 Context.stopService()来停止。
系统也会在组件不再被使用的时候戒者当Android必须为更多的活动组件回收内存时关闭它。稍后的组件的生命的周期一节,将对这种可能性及结果进行更详细的介绍讨论。
1.4 manifest文件
当Android打开一个应用程序组件时,它必须知道该组件是存在的。因此,应用程序会在一个被打包到Android包中的manifest文件中声明它的组件,.apk文件还将包含该应用程序的代码、文件以及其它资源。
manifest文件是一个结构化的XML文件,而且对于所有应用程序,文件名总是AndroidManifest.xml。除了声明此应用程序各个组件,它会做很多其他工作,比如指明应用程序所需链接到的库名称(除了默认的Android库外)以及标出应用程序所期望获得的各种权限。
但是manifest文件最重要的任务是向Android报告此应用程序的各个组件。丼例说明,一个activity可能声明如下:
lt;activitygt;元素的name属性指定了实现此activity的 Activity子类。icon和label属性指向包含展示给用户的此activity的图标和标签的资源文件。
其它组件也以类似的方法声明——lt;servicegt; 元素用于声明service, lt;receivergt; 元素用于声明broadcast receiver,而 lt;providergt; 元素用于声明content provider。未在manifest文件中进行声明的activity、service以及content provider将不为系统所见,从而也就永不会被运行。然而,broadca
全文共7250字,剩余内容已隐藏,支付完成后下载完整资料
资料编号:[142677],资料为PDF文档或Word文档,PDF文档可免费转换为Word
以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。