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

 2022-10-31 10:42:21

英文原文

Android Application Fundamentals

Android.developer

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 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:

1.The core framework components that define your application.

2.The manifest file in which you declare components and required device features for your application.

3.Resources that are separate from the application code and allow your application to gracefully optimize its behavior for a variety of device configurations.

1.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 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:

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 users contact information. As such, any application with the proper permissions can query part of the content pr

剩余内容已隐藏,支付完成后下载完整资料


中文翻译

Android应用程序基础

Android.developer

Android应用程序是用Java编程语言编写的。 Android SDK工具将代码以及任何数据和资源文件编译到Android包(一个.apk后缀的归档文件)中。 单个.apk文件中的所有代码都被认为是一个应用程序,是Android设备用来安装应用程序的文件。

安装在设备上后,每个Android应用程序都会位于自己的安全沙箱中:

Android操作系统是一个多用户Linux系统,而每个应用程序是一个不同的用户。

默认情况下,系统为每个应用程序分配一个唯一的Linux用户ID(该ID仅由系统使用,并且应用程序不知道)。 系统为应用程序中的所有文件设置权限,以便只有分配给该应用程序的用户ID才能访问它们。

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

默认情况下,每个应用程序都在其自己的Linux进程中运行。 当任何应用程序的组件需要执行时,Android启动该进程,然后当不再需要该进程时或当系统必须为其他应用程序恢复内存时关闭进程。

这样,Android系统实现了最小权限的原则。 也就是说,默认情况下,每个应用程序只能访问它所需的组件,而不能再执行其工作。 这创建了非常安全的环境,其中应用程序不能访问其没有被授予权限的系统的部分。

但是,有一种应用程序与其他应用程序共享数据的方法以及应用程序访问系统服务的方法:

可以安排两个应用程序共享相同的Linux用户ID,在这种情况下,他们能够访问对方的文件。 为了节省系统资源,具有相同用户ID的应用程序也可以安排在相同的Linux进程中运行并共享同一个VM(应用程序也必须使用相同的证书签名)。

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

这包括有关Android应用程序如何存在于系统中的基础知识。 本文档的其余部分将向您介绍:

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

2、在其中声明组件和应用程序所需的设备功能的清单文件。

3、与应用程序代码分离的资源,允许您的应用程序优化其针对各种设备配置的行为。

1、应用程序组件(Application Components)

应用程序组件是Android应用程序的基本组成部分。 每个组件是系统可以通过其进入应用程序的不同点。 并非所有组件都是用户的实际入口点,而一些依赖于彼此,但每个组件都作为其自身的实体存在,并起到特定的作用 - 每个组件都是一个唯一的构建块,可帮助定义应用程序的整体行为。

有四种不同类型的应用程序组件。 每种类型都具有不同的目的,并具有不同的生命周期,定义组件的创建和销毁方式。

这里有四种类型的应用程序组件:

活动(Activities)

活动表示具有用户界面的单个屏幕。 例如,电子邮件应用程序可能有一个活动显示新电子邮件的列表,另一个活动来撰写电子邮件,另一个活动用于阅读电子邮件。 虽然活动一起工作以在电子邮件应用程序中形成粘合的用户体验,但是每个活动独立于其他活动。 因此,不同的应用程序可以启动这些活动中的任何一个(如果电子邮件应用程序允许)。 例如,相机应用可以在构成新邮件的电子邮件应用中开始活动,以便用户共享图片。

活动作为Activity的子类实现,您可以在“活动”开发人员指南中了解更多信息。

服务(Services)

服务是在后台运行以执行长时间运行操作或执行远程进程工作的组件。 服务不提供用户界面。 例如,服务可能在用户处于不同应用程序中时在后台播放音乐,或者可能通过网络提取数据而不阻止用户与活动的交互。 另一个组件,如活动,可以启动服务,让它运行或绑定到它,以便与它进行交互。

服务作为服务的子类实现,您可以在服务开发人员指南中了解更多。

内容提供者(Content providers)

内容提供商管理应用数据的共享集合。您可以将数据存储在文件系统,SQLite数据库,Web上或应用程序可访问的任何其他持久存储位置。通过内容提供商,其他应用程序可以查询或甚至修改数据(如果内容提供商允许)。例如,Android系统提供管理用户的联系人信息的内容提供商。因此,具有适当权限的任何应用程序可以查询内容提供者的一部分(例如ContactsContract.Data)以读取和写入关于特定人的信息。

内容提供程序还可用于读取和写入应用程序专用的数据并且不共享。例如,笔记本示例应用程序使用内容提供程序保存笔记。

内容提供者被实现为ContentProvider的子类,并且必须实现一组标准的API,使得其他应用程序能够执行事务。有关详细信息,请参阅内容提供商开发人员指南。

广播接收器(Broadcast receivers)

广播接收机是响应于系统范围广播公告的组件。 许多广播源自系统 - 例如,广播通知屏幕已关闭,电池电量低或捕获了图片。 应用程序还可以启动广播 - 例如,让其他应用程序知道某些数据已下载到设备并可供他们使用。 尽管广播接收机不显示用户界面,但是它们可以创建状态栏通知以在广播事件发生时警告用户。 更常见的是,广播接收机只是一个“网关”到其他组件,并且旨在做非常少量的工作。 例如,它可以发起服务以基于事件执行一些工作。

广播接收器被实现为BroadcastReceiver的子类,并且每个广播作为Intent对象被传递。 有关更多信息,请参阅BroadcastReceiver类。

Android系统设计的一个独特方面是,任何应用程序都可以启动另一个应用程序的组件。例如,如果您希望用户使用设备相机捕获照片,则可能有另一个应用程序可以使用它,而不是开发一个活动来自己捕获照片。您不需要合并或甚至链接到来自相机应用程序的代码。相反,您可以在捕获照片的相机应用程序中启动活动。完成后,照片甚至会返回到您的应用程序,以便您可以使用它。对用户来说,好像摄像机实际上是应用程序的一部分。

当系统启动一个组件时,它会启动该应用程序的进程(如果它尚未运行),并实例化该组件所需的类。例如,如果应用程序在捕获照片的摄像头应用程序中启动活动,该活动将在属于相机应用程序的进程中运行,而不是在应用程序的进程中运行。因此,与大多数其他系统上的应用程序不同,Android应用程序没有单个入口点(例如,没有main()函数)。

因为系统在具有限制对其他应用程序的访问的文件权限的单独进程中运行每个应用程序,所以您的应用程序不能从另一个应用程序直接激活组件。然而,Android系统可以。因此,要激活另一个应用程序中的组件,您必须向系统发送一条消息,指定您启动特定组件的意图。然后系统为您激活组件。

每当出现一个需要被特定组件处理的请求时,Android会确保那个组件的应用程序进程处于运行状态,或在必要的时候启动它。并确保那个相应组件的实例的存在,必要时会创建那个实例。

2、激活组件(Activating Components)

四个组件类型中的三个 - 活动,服务和广播接收器 - 由称为意图的异步消息激活。 意图在运行时将各个组件彼此绑定(您可以将它们视为从其他组件请求操作的信使),无论组件是属于您的应用程序还是另一个组件。

使用Intent对象创建意图,Intent对象定义了一个消息来激活特定组件或特定类型的组件 - 意图可以分别是显式或隐式。

对于活动和服务,意图定义要执行的动作(例如,“查看”或“发送”某物),并且可以指定要采取行动的数据的URI(除了其他要素之外,启动的组件可能需要知道)。 例如,意图可以传达对活动显示图像或打开网页的请求。 在某些情况下,您可以启动一个活动来接收结果,在这种情况下,活动还会在Intent中返回结果(例如,您可以发出意向让用户选择个人联系人并将其返回给您 - 返回意图包括指向所选联系人的URI)。

对于广播接收器,意图简单地定义正广播的通告(例如,广播以指示设备电池低)仅包括指示“电池电量低”的已知动作串。

其他组件类型,内容提供程序不由意图激活。相反,当被来自ContentResolver的请求定向时,它被激活。 内容解析器处理与内容提供程序的所有直接事务,以便与提供程序执行事务的组件不需要,而是调用ContentResolver对象上的方法。 这在内容提供者和请求信息的组件之间留下了一层抽象(为了安全起见)。 有单独的方法激活每种类型的组件:

你可以通过传递一个Intent到startActivity()或startActivityForResult()(当你希望活动返回一个结果)时,开始一个活动(或给它一些新的东西做)。

您可以通过将Intent传递给startService()来启动服务(或向正在进行的服务提供新指令)。 或者您可以通过传递Intent tobindService()绑定到服务。

您可以通过将Intent传递到sendBroadcast(),sendOrderedBroadcast()或sendStickyBroadcast()等方法来启动广播。

您可以通过在ContentResolver上调用query()来对内容提供者执行查询。

有关使用意图的更多信息,请参阅意图和意图过滤器文档。 有关激活特定组件的更多信息还在以下文档中提供:Activities,Services,BroadcastReceiver和Content Providers。

组件声明

清单的主要任务是通知系统应用程序的组件。 例如,清单文件可以如下声明活动:

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

在lt;applicationgt;元素中,android:icon属性指向用于标识应用程序的图标的资源。

在lt;activitygt;元素中,android:name属性指定Activity子类的完全限定类名,android:label属性指定要用作活动的用户可见标签的字符串。

您必须以这种方式声明所有应用程序组件:

1、lt;activitygt;活动元素

2、lt;servicegt;元素的服务

3、lt;广播接收机的lt;receivergt;元素

4、内容提供者的lt;providergt;元素

您在源中包括但未在清单中声明的活动,服务和内容提供程序对系统不可见,因此永远不会运行。 然而,广播接收器可以在清单中声明或在代码(作为BroadcastReceiver对象)中动态创建,并通过调用registerReceiver()向系统注册。

3、声明组件的能力

如上所述,在激活组件中,可以使用Intent启动活动,服务和广播接收器。您可以通过在intent中明确命名目标组件(使用组件类名称)来实现。然而,意图的真正力量在于意图动作的概念。通过意向操作,您只需描述要执行的操作的类型(以及可选择要执行操作的数据),并允许系统在设备上查找可以执行操作并启动的组件它。如果有多个组件可以执行由意图描述的操作,那么用户选择使用哪一个。

系统识别可以响应意图的组件的方式是通过将接收到的意图与在设备上的其他应用的清单文件中提供的意图过滤器进行比较。

当您在应用程序清单中声明组件时,可以选择包括声明组件功能的意图过滤器,以便它可以响应来自其他应用程序的意图。您可以通过添加一个lt;intent-filtergt;元素作为组件的声明元素的子元素为您的组件声明一个intent过滤器。

例如,具有用于撰写新电子邮件的活动的电子邮件应用可以在其清单条目中声明意图过滤器以响应“发送”意图(以便发送电子邮件)。然后,应用程序中的活动可以使用“发送”操作(ACTION_SEND)创建意图,系统与电子邮件应用程序的“发送”活动相匹配,并在您使用startActivity()调用意图时启动它。

有关创建意图过滤器的更多信息,请参阅意图和意图过滤器文档。

声明运行程序所需的条件

有多种由Android供电的设备,并不是所有设备都提供相同的功能和功能。为了防止应用程序安装在缺少应用程序所需功能的设备上,请务必通过在清单文件中声明设备和软件要求,为应用程序支持的设备类型定义配置文件。这些声明大多数仅供参考,系统不会读取,但Google Pla

剩余内容已隐藏,支付完成后下载完整资料


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

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

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