安卓应用基础外文翻译资料

 2022-05-23 09:05

Android Application Fundamentals

Android applications are written in the Java programming language. The Android SDK tools compile the code—along with any data and resource files—into 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 application#39;s 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 application#39;s components need to be executed, then shuts down the process when it#39;s 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:

  • It#39;s possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each other#39;s 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 user#39;s 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 Components

Application 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 role—each one is a unique building block that helps define your application#39;s 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:

Activities

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

Services

A 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 providers

A 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 user#39;s contact information. As such, any application with the proper permissions can query part of the content provider (such as ContactsContra

全文共22682字,剩余内容已隐藏,支付完成后下载完整资料


安卓应用基础

安卓应用程序是基于Java编程语言所编写,其主要是用安卓的SDK编译代码工具将所有的数据与源文件并入一个安卓的封装,一个有.apk后缀的归档文件,所有在一个单一.apk文件的代码被认为是一个应用程序,并且是一个安卓电子设备用来安装应用程序的文件。

一旦安装在设备上,每个安卓应用程序将在它自己的安全沙箱中运行:

安卓操作系统是一个每个应用程序对应不同的用户的多用户Linux系统。

默认情况下,每个应用程序的系统分配一个唯一的Linux用户ID(该ID仅用于由系统是未知的应用程序),系统设置所有的应用程序中的文件权限,以便只有分配给该应用程序的用户ID可以访问它们。

每个进程都有它自己的虚拟机(VM),因此应用程序的代码的运行与其他应用程序隔离。

默认情况下,每个应用程序在它自己的Linux进程中运行。当应用程序的任何组件需要被执行时,安卓启动进程。然后当它不再被需要或系统必须从其他应用中恢复记忆时,进程结束。

这样一来,安卓系统实现了最小特权原则,也就是说,每个应用程序,默认情况下,只能访问其需要做工作的组件,并没有其他例外。这将创建一个非常安全的环境,在这环境之中程序无法访问那些其未被给与许可的部分系统。

但是,有一些方法使应用程序与其他应用程序分享数据和并使应用程序访问系统服务:

为两个应用程序安排共享相同的Linux用户ID是有可能的,在这种情况下,它们能够相互访问对方的文件。为了节约系统资源,使用相同的用户ID的应用程序还可以安排运行于相同的Linux进程和共享同一个虚拟机(这些应用也必须使用相同的证书签名)。

应用程序可以请求设备的访问权限,如用户的联系人,短信,可安装存储(SD卡),摄像头,蓝牙等设备的数据,所有应用程序的权限必须由用户在应用安装时授予。

这就基本涵盖了安卓应用程序如何在系统中存在。

这篇文章的其余部分将向您介绍:

定义应用程序的框架核心组件。

声明组件和需求设备功能的应用程序的清单文件。

与应用程序代码分开的资源,并允许您的应用程序的操作以精确优化各种设备配置。

应用程序组件(Application Components)

安卓的核心功能之一就是一个应用程序可以使用其它应用程序的组件(如果那个应用程序允许的话)。并不是所有组件都是用户的实际接入点,有些组件是相互依赖的。但是每一个组件都作为它自己的实体而存在,并扮演着一个特定的角色,每一个组件都是一个独特的构建块,并有助于定义应用程序的整体行为。

为达到这个目的,系统必须在一个应用程序的一部分被需要时启动这个应用程序,并将那个部分的Java对象实例化。与在其它系统上的应用程序不同,安卓应用程序没有为应用准备一个单独的程序入口(比如说,没有main()方法), 而是为系统依照需求实例化提供了基本的组件。

共有四种不同的应用程序组件。每种类型的组件都有一个明确的目标,并且有一个明确的生命周期定义了组件是如何创建和销毁的。

下面共有四种组件类型:

活动(Activities)

一个 活动 代表用户界面的一个独立屏幕。例如,一个邮件应用程序应该有一个活动用于显示新邮件列表,另一个活动用于撰写一封邮件,还有一个活动用于读取邮件。尽管所有活动协同工作以构成邮件应用程序的用户体验,但彼此之间相对独立。因此,一个不同的应用程序能够从任何一个活动启动 (只要邮件应用程序允许)。例如,用户需要分享一张照片,一个拍照应用程序能够启动邮件应用程序的活动。

活动是一个实现了 活动 的子类,你可以在活动 开发者指导部分了解更多。

服务(Services)

服务是在后台运行的一个执行长时间操作或者执行远程操作的组件。服务不提供用户界面。例如,当用户在使用另一个应用程序时,一个服务可在后台播放音乐,或者是从网络上获取数据同时不阻断用户与当前activity的交互。其他组件,比如一个活动,为了与该服务互动,可以启动或者 绑定它。

服务 是一个实现了服务 的子类,你可以在 服务 开发者指导部分了解更多。

内容提供者(Content providers)

内容提供者管理着一些可分享的应用数据。数据可以存储于文件系统、SQLite数据库,网络,或其它应用可以获取的持久的储存位置。通过内容提供者,其他应用程序可以查询甚至修改数据(如果内容提供者允许)。例如,安卓系统提供了一个内容提供者来管理用户的联系信息。因此,任何拥有适当权限的应用程序可以查询内容提供者的部分(如交互。数据)来读取和写入一个特定的人的信息。

内容提供者对于私有且不开放的应用的数据的读取和写入也很有用。例如,便笺样例应用程序能使用内容提供程序来保存注释。

参阅独立的内容提供者Content Providers 章节获得更多关于使用内容提供者的内容。

广播接收器(Broadcast receivers)

广播接收器是一个专注于接收广播通知信息,并做出对应相应的组件。很多广播是源自于系统的──比如,通知屏幕关闭、电池电量低、拍摄了一张照片等。应用程序也可以进行广播──比如说,通知其它应用程序一些数据已下载完成并处于可用状态。虽然广播接收器不显示用户界面,但他们可以创建一个状态栏通知,以在广播事件发生时通知用户。然而,更常见的是,广播接收器只是对其他组件的“网关”,其目的是进行极少量的工作。例如,它可能启动一个服务来根据事件执行一些工作。

广播接收器是一个广播接收器的子类,每个广播都是一个Intent对象传递的实现。想了解更多信息的话,见广播接收器部分。

安卓系统设计的一个独特方面是任何的一个程序都可以启动另一程序的组件。比如,你想让你的程序可以使用照相机拍照,如果已经有了实现这种功能的程序并且你的程序能使用它(有权限)而不是你自己再写一个新的活动来实现这个功能。你的程序不需要包含或者链接这个拍照程序。相反,你只需要在你的程序中打开这个拍照程序中的实现拍照功能的活动。当拍完之后,拍好的照片甚至会自动返回给你的程序。这对于用户来说,就好像是想拍照功能就是你的这个程序的一部分一样。

当系统启动一个组件之后,如果这个组件所在的程序之前没有运行的话,系统会自动开始这个程序的进程,并初始化这个组件所需要的相关类。比如,你的程序开启了一个拍照功能程序的活动,这时系统会启动这个活动所在的程序,所以这个活动运行在拍照功能的程序当中,而不是在你的程序中。所以,不像其他操作系统的中的程序一样,安卓程序没有一个单独的入口点(比如没有我们常见的main()函数)。

因为系统中的程序运行在自己的独立进程中,并且程序中的文件都有自己的限制其他程序访问的权限,所以,你的程序不能直接激活其他程序中的组件。但是安卓系统就可以。为了激活其他程序中的组件,你必须向系统发送一个消息来详细说明你要启动其他组件的意图,这样系统才会为你激活这个组件。

激活组件(Activating Components)

四大组件中的三个组件——活动、服务和广播接受器——是由一种叫intent的异步消息来激活的。这些intents在运行时将这些属于你的程序或不同程序的独立的组件绑定在一起,你可以把这些intents看作是需要其他组件的操作的信使。无论这个组件是属于你的或是其他的应用。

一个intent就是一个Intent对象,这个intent定义了一种可以激活某个特定组件或者某种特定类型的组件的信息,这两种情况分别对应两种intent的定义方式,显示的或者隐式的。

对于活动和服务,一个intent定义了要执行的操作 (比如,要“看”或者“发送”什么)和要操作的数据的URI(要启动该组件可能还需要知道其他信息)。比如,一个intent可能会为一个活动传递一个请求来展示一张图片或者打开一个网页。有时,你可以启动一个活动来得到返回的结果,在这个例子中这个活动的返回的结果也是一个Intent(比如,你可以发送一个intent让用户选择一个个人接触并返回给你——这个返回的intent就包含了一个指向用户选择的联系人的URI)。

对于广播接收者来说,intent只是简单的定义了要广播的内容(比如,一个用以表明电池电量很低的广播仅包含了一个表明电池电量很低的字符串)。

最后一种组件类型内容提供者并不是由intent来激活的(activate)。而是由接收到内容解析器的请求时激活的。内容解析器处理所有直接交互是通过内容提供商的从而使部件的提供者不需要执行交互,而不是通过响应内容解析器对象的方法。这就在内容提供者和请求信息的组件之间留下了一层抽象层(用于安全性)。

它们都各自有自己的方法来激活相应的组件:

  • 你可以通过传递一个Intent给startActivity()或startActivityForResult()启动一个活动(或者给他一些新的要做的内容)。使用startActivityForResult()你将得到一个返回结果。
  • 你可以通过传递一个Intent给startService()来start一个服务(或者给一个正在运行的服务一些新的指令。或者你可以通过把一个Intent传递给bindService()来绑定一个服务。
  • 你可以通过传递一个Intent给诸如sendBroadcast()、sendOrderedBroadcast()或者sendStickyBroadcast()等方法来初始化一个广播。
  • 你可以通过调用内容解析器的query()方法来执行一次内容提供者的查询操作。

更多的关于intent的内容,可以参看文档中的Intents and Intent Filters。更多的关于激活特定组件的内容可以参看文档中的:活动、服务、广播接收器、内容提供者。

关于Manifest文件(The Manifest File)

在安卓系统可以启动一个应用程序组件之前,安卓系统必须通过读取这个程序的AndroidManifest.xml(即manifest文件)文件来确定要启动的组件存在。你的程序必须在这个manifest文件声明用到的所有的组件,并且这个manifest文件必须在项目的根目录下。

另外,这个manifest文件还声明一些其他的东西,比如:

  • 确定这个程序需要的所有权限,比如Internet访问权限或者读取用户联系人权限。
  • 声明这个运行这个程序所需要的最低API版本,根据开发该程序所使用的API版本。
  • 声明该程序所需要的硬件或软件特征,比如照相机、蓝牙服务或者多点触屏。
  • 声明该程序需要链接(link against)的API库(不是安卓的framework APIs),比如Google Maps library。
  • 等等。

组件声明

Manifest文件的首要任务就是通知系统关于程序中要使用的组件。比如,一个manifest文件可以用如下的方式来声明一个活动:

[java] view plaincopy

  1. lt;?xml version='1.0' encoding='utf-8'?gt;
  2. lt;manifest ... gt;
  3. lt;application android:icon='@drawable/app_icon.png' ... gt;
  4. lt;activity android:name='com.example.project.ExampleActivity'
  5. android:label='@string/example_label' ... gt;
  6. lt;/activitygt;
  7. ...
  8. lt;/applicationgt;
  9. lt;/manifestgt;

在lt;applicationgt;元素中,android:icon属性用于指定一个用于标示该程序的icon。

在lt;activitygt;元素中,android:name属性用于确定这个扩展自活动的子类的全路径名,android:label属性用于标示这个活动的对于用户可见的标记。

你必须要用以下方式来声明你的程序组件:

  • activities:lt;activitygt;标签
  • services:lt;servicegt;标签
  • broadcast receiver:lt;receivergt;标签
  • content providers:lt;providergt;标签

如果程序中用到活动、服务和内容提供者,但未在manifest文件中声明

全文共8219字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[11881],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。