222外文翻译.doc_第1页
222外文翻译.doc_第2页
222外文翻译.doc_第3页
222外文翻译.doc_第4页
222外文翻译.doc_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

外 文 翻 译题 目: Android应用程序介绍 Android应用程序介绍【Application Fundamentals】Android应用程序使用Java语言编写。Android SDK工具编译代码(包括任何数据和资源文件),将其打包成Android package,一种以.apk结果的存档文件。所有的代码只是一个.apk文件,培训班被认为是一个应用程序,并且Android授权应用程序使用它来安装应用程序。一旦在一台设置上安装,每个应用程序生存在它自己的沙箱:1、Android是一个多用户的Linux系统,其中每一个应用程序都是一个不同的用户。2、默认系统赋予每个应用程序一个与惟一的Linux user ID(ID只被系统使用,对应用程序不可见)。系统设置给应用程序所有文件设置权限,使得只有被该应用程序的user ID才能访问他们。3、每个进程有他们各自的虚拟机(VM),所以每个应用程序独立于其它应用程序运行。4、默认每个应用程序在它自己的Linux进程中运行。Android开户进程当进程的任意一个组件需要被执行的时候,然后关闭进程当它不再被需要的时候或系统需要恢复内存给其它应用程序使用的时候。按照这种方式,Android实现了“priciple of least privilege”,也就是,每个应用程序默认只能访问他要求用来工作的组件,除此之外,什么也没有。这种机制创建一个非常安全的环境,使得一个应用程序无法访问系统中没有授权的部门。然后,仍然有让一个应用程序和其它应用程序分享数据的方法,也有让一个应用程序访问系统服务的方法:1、两个应用程序可以共享同一个Linux User ID,这样他们就能相互访问对方的文件。为了保护系统资源,具体同样Linux UID的应用程序被安排同一个进程中,并且共享同一个VM(另外他们必须有同样的签名证书)。2、一个应用程序可以请求访问在设备数据例如用户联系人、SMS消息、SK卡、照相机、蓝牙以及更多,但是必须在安装时得到用户的授权。上面叙述了应用程序怎样在Android中存在。【Application Components】应用程序组件是Android应用程序的基本构建块。每个组件是一个系统能够进入你应用程序的入口点。对用户来说并不是所有的组件都是实际的入口点,而是其中一些依赖于另一些,但是每个都有自己的实体的存在,并且扮演着一个独特的角色每一个都是帮助你定义自己应用程序整体行为的一个单独构建块。有4种不同的应用程序组件,每一种都提供不同的服务,每一种都一个不同的生命周期,生命周期定义了组件的创建和消亡。接下来介绍上述的4种不同的应用程序组件。【Activities】 一个activity代表一个拥有一个用户接口的屏幕。例如,电子邮件应用程序可能有一个activity展现邮件列表,另一个activity构建email,还有一个用来阅读activity。虽然在邮件应用程序中activities一起工作来展现一个连续的用户体验,但每一个都是相对于其它的都独立。这样,不同的应用程序可以以任何一个activity开始(如果邮件应用程序允许)。例如,一个照相应用程序可以以邮件应用程序中的构建新邮件的activity来启动,目的是让用户分享邮件。一个activity是一个实际了Activity的子类。【Services】一个Service是一个运行在后台以运行长时间操作或者是实现远程操作的行为的组件。一个Service没有提供用户服务。例如,当一个用户在另运行另一个应用程序时音乐播放Service可以在后台运行,或者是从网络获取数据而不打断当前activity与用户的交互。另一个组件,例如activity,可以开始一个Service然后证它工作或者绑定它而与它交互。一个Service是一个实现了Service的子类。【Content providers】一个Content provider管理器管理应用程序分享的数据集。你可以存储这些数据在文件系统、一个SQLite数据库、在Web蔌者是任何其它的你的应用程序可以访问的永久性存储设备。通过content provider,其它应用程序可以查询甚至修改数据(如果content provider允许)。例如,Android系统提供一个content provider来管理用户的联系人信息。这样的话,任何一个拥有合适权限应用程序可以查询content provider的一部来然后来读和写关于某个人的信息。Content providers同样在读和写一个应用程序私有数据时很有用。例如,一个content provider是一个实现了ContentProvider类的子类,并且必须实现一个标准API集以允许其它应用程序行使事务。【Broadcast receiver】一个Broadcast receiver是一个响应系统级广播通知的组件。很多的广播源于系统,例如,一个通知屏幕已经关闭的广播、电量很低的广播、图片锁定的广播。应用程序同样可以初始化广播例如,让其它应用程序知道一些数据已经被下载到设备上并且可以被他们使用。虽然broadcast receivers没有用户接口,他们可以建立一个状态条的通知通知用户一个广播事件发生。更普通的是,一个broadcast receiver对其它组件来说是一个网关(gateway),只做很少一部分工作。例如,它初始化一个苦于某个事件的服务去完成一项工作。一个broadcast receiver实现BroadcastReceiver,并且每个broadcast被Intent对象传递。 Android系统调序的一个特别之处是任何一个应用程序可以开启另一个应用程序的组件。例如,如果你想要用户使用摄像头拍一张相片,可以另一个程序有这个功能,然后你的应用程序是使用它,而不是开发一个activity用拍摄一张相片。你不需要组合和连接摄像应用程序的代码。反而,你可以简单的启动摄像应用程序的拍照activity。当完成的时候,相片被返回到你的应用程序然后你可以使用它。对用户来讲,他感觉报像是你应用程序的一部分。当系统启动一个组件,它为该应用程序新建一个进程(如果该进程不在运行的话),并表示例这个组件需要的类。例如,如果你的应用程序开启一个摄像机应用程序的activity来拍照,那个activity运行在摄像机应用程序的进程中,不是在你的应用程序进程中。因此,和其它操作系统的应用程序不一样,Android应用程序没有一个简单的入口点(entry point)(也就是没有main()函数)。因为系统运行第一个应用程序在一个单独的进行中,并且赋予不同的权限以限制访问其它应用程序,你的应用程序不能直接激活其它应用程序的组件。可是,Android系统可以。所以,要激活其它进程中的组件,你必须发送一个消息给系统,指明你的意图(intent)为启动某一个组件。系统可以为你激活组件。【Activating Components】4种组件类中的3种activities、services、broadcast被一个称为intent的异步消息激活。Intents让单独的组件在运行时绑定在一起(你可以把他们看作从其它组件请求行为的消息),不管那个组件属不属于你的应用程序。intent被Intent对象创建,用来定义一个消息以启动一个指定组件或者是指定类型的组件一个Intent可以是显示的或隐式的。对activities和services来讲,一个intent定义了执行的行为(例如,查看或发送某个事情),可以指定要操作的URI(组件启动需要知道的事情)。例如,一个intent可以发送一个请求给activity来展现一个图片蔌者打开一个web页面。在某些情况下,你可以启动一个activity来接收结果,在这种情况下,请求的activity也返回结果在一个intent中(例如,你可发出一个intent来让用户选择一个用户联系人,然后返回给你这个返回的intent包含一个指定选择联系人的URI)。对broadcast receivers,intent仅简单的定义被广播的声明(例如,一个设备电量低的广播只包括一个已经的行为字符串代表“battery is low”)。其它的组件类型,content provider,不是被intent激活。相反,它在被一个来自ContentResolver的请求盯上的时被激活。ContentResolver操作所有和content provider直接的事务,而实现事务的组件不需要和ContenResolver通信。这使得在content provider和component请求者之间留了一个虚拟的层(出于全安)。有单独的方法来激活各种类型的组件:1、你可以开始一个activity通过传递一个Intent给startActivity()或startActivityForResult()(当前需要activity 返回返回一个结果)。2、你可以开户一个service(或者给当前运行service新指示)通过传递一个Intent给startService(),或者你可以绑定到service通过传递一个Intent给bindService()。3、你可以初始化一个broadcast通过传递一个Intent给方法例如sendBroadcast()、sendOrderedBroadcast()或者sendStickyBroadcast()。4、你可以实现查询一个content provider通过调用ContentreSolver的query()函数。【The Manifest File】在Android系统能够启动一个应用程序组件之前,系统必须通过阅读应用程序AndroidManifest.xml文件来知道组件的存在。你的应用程序必须声明它所有的组件在这个文件中,必须放在应用程序项目根目录。Manifest文件做一系列事情包括声明应用程序组件,例如:1、认证任何应用程序请求的用户权限,例如网络访问或者读用户联系人的权限。2、声明应用程序所需要的最小的API级别。3、声明该应用程序使用的硬件或软件的特点,例如摄像头、蓝牙或多等触摸。4、需要链接的API库,例如Google Maps Library。5、其它【Declareing components】Manifest的主要的任务是通知系统关于应用程序的组件信息。例如,一个manifest文件可以声明一个activity像下面这样:在元素中,android:icon属性指定一个用来标识应用程序的图标。在元素中,android:name属性指明了Activity子类的全称,android:label属性指定一个字符串作为用户可见的activity标签。你必须像下面一样声明所有的应用程序组件:1、元素声明activities2、元素声明services3、元素声明broadcast receivers4、元素声明content provider你代码中无声明的Activities、services、和content providers对系统是不可见的,并且相反地,永远都无法运行。可是,broadcast receivers可以被声明在manifest中或者用代码动态(就像BroadcastReceiver对象)并且注册进系统通过registerReceiver()。【Declaring component capabilities】就像上面讨论的,在activiting组件,你可以使用Intent来启动activities、service、和broadcast receiver。你可以通过显示地命名目标组件(使用组件类名)在intent中。然而,intent的真下能力在于intent actions的概念。在intent actions,你简单的描述你想要执行的动作类型(或者是你希望实现操作的数据)并且允许系统找一个设备上的组件来实现action并且启动它。如果有多个组件能完成那个intent描述的动作,那么由用户选择哪一个来用。(PS:像windows注册表和扩展名的概念)系统辨别哪个组件能响应一个intent通过比较intent filters,被设备上的其它应用程序的manifest文件提供。当你在应用程序manifest中声明一个组件时,你可选的可以包含intent filters,用来声明你组件的能力,以致它可以响应来自其它应用程序的intent。你可声明一个intent filter给你的组件通过添加元素作为组件声明元素的子元素。例如,一个邮件应用程序有一个activity用来组建一个新邮件,或者会声明一个intent filter在它的manifest入口点来响应发送intents(为了发送邮件)。那一个Activity在你应用程序里可以创建一个intent带上“发送”动作(ACTION_SEND),系统匹配邮件应用程序的“发送”activity,然后用startActivity()启动它。【Declaring application requirements】有各种各样的设备被Android管理,并不是他们中的所有都提供相同的特点和能力。为了防止你的应用程序安装在缺少设备环境的机器上,所以清晰的定义一个你的应用需要的设备和软件要求在你的manifest中显得非常重要。它们中大多数只是传达信息,系统不会阅读它们,但是外部services例如Android Market会阅读他们目的是给用记提供过滤当他们搜索应用程序在他们的机器上时。例如,如果你的应用程序要求一个摄像头然后使用2.1的API(Level 7),你应该声明 这些作为要求在你的manifest文件里。这样的话,没有摄像头的和Android version小于2.1的机器就不能安装Android Market。可是,你也可以声明你的应用程序使用摄像头,但并不一定要求。在这种情况下,你的应用程序在运行时会检查以决定是不否这个设备有一个摄像头并且关闭所有使用使用摄像头的功能在没有摄像头的情况下。下面是一些重要的设备特性你应该考虑的在你设计和开发你的应用程序的时候。1、Screen size and density为了分类设备的屏幕类型,Android为每种设备定义了两种属性:屏幕大小(屏幕物理尺寸)和屏幕分辨率(屏幕物理像素密度或每英寸DPI-DOT)。为了简化各种类型屏幕的配置,Android系统把他们生来可选择的组让他们更容易被定位。屏幕大小是:small, normal, large 和extra larege.屏幕分辨率是:low density, medium density, hidensity 和extra hight density默认,你的应用程序兼容所有的屏幕大小和分辨率,因为android系统生成全适的调整对你的UI布局和图像资源。可是,你应该建立独特的布局为某一种特定的屏幕大小并且提供特定的图片为特定的密度,使用可选的布局和资源,并且声明在你的manifest文件中哪个屏幕大小你的应用程序支持通过元素。2、Input configurations很多设备提供不同类型的输入机制,例如硬件键盘,跟踪球,或者是five-way浏览器。如果你的应用程序要求一个特别各类的输入硬件,那么你应该声明它在你的manifest文件中通过元素。可是,极少数情况下一个应用程序应该声明某种输入配置。3、Device features会有很多的硬件和软件特性可能存在或不存在在一个给定的android设备上,例如摄像头、光感器、蓝牙、某种版本的OpenGL、或者是触摸屏的精确性。你应该永远不假设某种特性在Android设备上具有(不是说标准Android库),所以你应该声明任何特点你应用程序会使用到的通过元素。4、Platform Version不同的Android设备通常运行不同版本的Android平台,例如Android1.6或Android 2.每个连续的版本通常包含不存在于版本的额外的APIs。为了指定哪个API集合是可用的,每个平台版本指定一个API Level(例如,Android 1.0 是API Level 1并且Android 2.3是 API Level 9)。如果你使用任何1.0版本以后的API,你应该声明最小的API Level使用元素。你声明所有的要求为你的应用程序非常重要,因为,当你发布你的应用程序在Android Market时,Market使用这些声明来过滤哪些程序在各个设备上是可用的。这校报话,你的应用程序将出现在符合你所有要求的设备上。 一个Android应用不仅仅由代码组件它要求独立于代码的资源,例如图像、音频文件以及任何涉及到应用程序视觉表达的东西。例如,你可以定义动画、菜单、颜色、以及activity用户接口的布局用XML文件。使用应用程序资源使得它很容易你应用程序的多变的特性而不需要修改代码并且能过提供一系统可选的资源,使得你能优化你的应用程序为一各种不同的设备配置(例如不同语言和不同屏幕大小)。每一个引入你应用程序的资源,SDK编译工具定义一个惟一的整数ID,你可以用来引用资源从你的应用程序代码里或定义在XML中的其它资源。例如,如果你的应用程序包含一个图像文件名字为logo.png(保存在/res/drawable/directory),SDK工作生成一个资源ID名叫R.drawable.log,你可以使用来引用 图片和插入它到你的用户接口里。最重要一个方面是提供资源给你的源代码是给你的应用程序提供为不同设备提供不同资源的能力。例如,通过定义UI字符串在XML,你可以转换这些字符串到别的语言并且保存这些字符串在单独的文件中。然后,根据语言qualifier,你添加资源到资源目录的名字(如res/values-fr/ 为法语字符串值)并且用户的语言选项,Android系统应用合适的语言选项为你的UI。Android支持多种不同的qulifiers为你的可选资源。qualifier是一个短字符串,以资源目录作为名字,你包含为了定义设备配置哪个资源应该被使用。另一个例子,你应该经常定义不同的布局为你的activities依赖于设备屏幕的方向和大小。例如,当设备屏幕竖立的时候(高),你或许希望一个希望按钮垂直的按钮,但是当屏幕在浏览状态下(宽),按钮应该水平对齐。为了改变布局依赖于方向,你可以定义两种不同的布局就胳合适的qualifier给每个而已目录。然后,系统自定应用合适的布局依赖于当前的设备方向。 原文出处:Google Company. Android api document. Application Fundamentals EB/OL. 2012.5./guide/topics/fundamentals.html. Android Application IntroductionApplication FundamentalsAndroid applications are written in the Java programming language. The Android SDK tools compile the codealong with any data and resource filesinto an Android package, an archive file with an .apk suffix. All the code in a single .apk file is considered to be one application and is the file that Android-powered devices use to install the application.Once installed on a device, each Android application lives in its own security sandbox:The Android operating system is a multi-user Linux system in which each application is a different user.By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.Each process has its own virtual machine (VM), so an applications code runs in isolation from other applications.By default, every application runs in its own Linux process. Android starts the process when any of the applications components need to be executed, then shuts down the process when its no longer needed or when the system must recover memory for other applications.In this way, the Android system implements the principle of least privilege. That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.However, there are ways for an application to share data with other applications and for an application to access system services:Its possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each others files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate).An application can request permission to access device data such as the users contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All application permissions must be granted by the user at install time.That covers the basics regarding how an Android application exists within the system. The rest of this document introduces you to:The core framework components that define your application.The manifest file in which you declare components and required device features for your application.Resources that are separate from the application code and allow your application to gracefully optimize its behavior for a variety of device configurations.Application ComponentsApplication components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific roleeach one is a unique building block that helps define your applications overall behavior.There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.Here are the four types of application components:ActivitiesAn activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.An activity is implemented as a subclass of Activity and you can learn more about it in the Activities developer guide.ServicesA service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.Content providersA content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications can query or even modify the data (if the content provider allows it). For example, the Android system provides a content provider that manages the users contact information. As such, any application with the proper permissions can query part of the content provider (such as ContactsContract.Data) to read and write information about a particular person.Content providers are also useful for reading and writing data that is private to your application and not shared. For example, the Note Pad sample application uses a content provider to save notes.A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other applications to perform transactions. For more information, see the Content Providers developer guide.Broadcast receiversA broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the systemfor example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcastsfor example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers dont display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a gateway to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.A unique aspect of the Android system design is that any application can start another applications component. For example, if you want the user to capture a photo with the device camera, theres probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. You dont need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo. When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application.When the system starts a component, it starts the process for that application (if its not already running) and instantiates the classes needed for the component. For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your applications process. Therefore, unlike applications on most other systems, Android applications dont have a single entry point (theres no main() function, for example).Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application. The Android system, however, can. So, to activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.Activating ComponentsThree of the four component typesactivities, services, and broadcast receiversare activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another.An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of componentan intent can be either explicit or implicit, respectively.For activities and services, an intent defines the action to perform (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论