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

 2022-08-12 03:08

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—intoan 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 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 provider (such as ContactsContract.Data) to

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


安卓应用基础

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应用程序在系统中如何存在的基础知识。本文档的其余部分向您介绍:

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

清单文件,您可以在其中声明应用程序的组件和必需的设备功能。

与应用程序代码分离的资源,使您的应用程序可以针对各种设备配置适当地优化其行为。

应用程序组件(Application Components)

应用程序组件是Android应用程序的基本构建块。每个组件都是一个不同的点,系统可以通过该点进入您的应用程序。并非所有组件都是用户的实际入口点,并且某些组件是相互依赖的,但是每个组件都作为自己的实体存在并扮演特定的角色——每个组件都是一个独特的构建块,可帮助定义应用程序的整体行为。

有四种不同类型的应用程序组件。每种类型都有不同的用途,并具有不同的生命周期,该生命周期定义了如何创建和销毁组件。

以下是四种类型的应用程序组件中的三个:

活动(Activities):

activity代表具有用户界面的单个屏幕。例如,电子邮件应用程序可能有一个activity显示新电子邮件的列表,另一个activity用于撰写电子邮件,另一个activity用于阅读电子邮件。尽管这些activity可以协同工作以在电子邮件应用程序中形成整体的用户体验,但是每个activity都彼此独立。这样,其他应用程序可以启动这些activity中的任何一项(如果电子邮件应用程序允许的话)。例如,摄像机应用程序可以在撰写新邮件的电子邮件应用程序中启动activity,以便用户共享图片。

activity是一个实现了 Activity 的子类,你可以在“ Activities 开发者指导”部分了解更多。

服务(Services):

service在后台运行,执行长时间操作或者执行远程操作。service不提供用户界面。例如,service可能会在用户处于其他应用程序中时在后台播放音乐,或者可能会在不阻止用户与服务交互的情况下通过网络获取数据。另一个组件(例如activity)可以启动service,并使其运行或绑定到该activity,以便与其进行交互。

service是一个实现了 Service 的子类,你可以在“ Services 开发者指导”部分了解更多。

内容提供者(Content providers):

content provider将一些特定的应用程序数据供给其它应用程序使用。您可以将数据存储在文件系统,SQLite数据库,Web上或应用程序可以访问的任何其他持久性存储位置中。通过content provider,其他应用程序可以查询甚至修改数据(如果content provider允许的话)。例如,Android系统提供了一个content provider,用于管理用户的联系信息。这样,具有适当权限的任何应用程序都可以查询内容提供者的一部分(例如ContactsContract.Data)以读取和写入有关特定人员的信息。

content provider对于读取和写入对您的应用程序私有但未共享的数据也很有用。 例如,“记事本”示例应用程序使用content provider来保存注释。

content provider作为ContentProvider的子类实现,并且必须实现一组标准的API,这些API可使其他应用程序执行事务。有关更多信息,请参阅Content Provider开发人员指南。

Android系统设计的独特之处在于,任何应用程序都可以启动另一个应用程序的组件。例如,如果您希望用户使用设备相机拍摄照片,则可能有另一个应用程序可以执行该操作,而您的应用程序可以使用它,而不是自己开发activity来拍摄照片。您不需要合并甚至链接到相机应用程序中的代码。相反,您可以简单地在捕获照片的相机应用程序中启动活动。完成后,照片甚至会返回到您的应用程序,以便您使用。对于用户来说,相机似乎是您应用程序的一部分。

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

因为系统中的程序运行在自己的独立进程中,并且程序中的文件都有自己的限制其他程序访问的权限,所以,你的程序不能直接激活其他程序中的组件。但是,Android系统可以。因此,要在另一个应用程序中激活组件,您必须向系统传递一条消息,传达您打算启动特定组件的意图。然后,系统会为您激活该组件。

激活组件(Activating Components)

四种组件中的三种——activities, services和broadcast receivers由称为intent的异步消息激活。Intent在运行时将各个组件彼此绑定(您可以将它们视为从其他组件请求操作的使者),无论该组件属于您的应用程序还是属于其他的应用程序。

使用Intent对象创建一个Intent,该对象定义了激活特定组件或特定类型的组件的消息——Intent可以是显式的也可以是隐式的。

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

对于broadcast receivers,此意图仅定义了要广播的公告(例如,用于指示设备电池电量低的广播仅包括指示“电池电量低”的已知操作字符串)。

另一个组件类型,content provider,不会被intent激活。而是当它被ContentResolver的请求作为目标时被激活。内容解析器处理与content provider的所有直接交互,因此与content resolver进行交互不需要intent,而是调用ContentResolver对象上的方法。这就在content resolver和请求信息的组件之间保留了一层抽象(出于安全性考虑)。

有激活每种组件的单独方法:

您可以通过将intent传递给startActivity()或startActivityForResult()来启动activity(或给它做一些新的事情)(当您希望activity返回结果时)。

您可以通过将intent传递给startService()来启动service(或为正在进行的service提供新的说明)。或者,您可以通过将intent传递给bindService()来绑定到service。

您可以通过将intent传递给sendBroadcast(),sendOrderedBroadcast()或sendStickyBroadcast()之类的方法来启动广播。

您可以通过在ContentResolver上调用query()对content provider执行查询。

有关使用intent的更多信息,请参考“Intents和Intent Filters”文档。以下文档中还提供了有关激活特定组件的更多信息:Activity,Service,BroadcastReceiver和Content Providers。

声明组件(Declaring components)

清单的主要任务是通知系统有关应用程序组件的信息。 例如,清单文件可以声明activity,如下所示:

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属性指定一个字符串,用作该活动的用户可见标签。

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

lt;activitygt; activity元素

lt;servicegt; service元素

lt;receivergt; broadcast receivers元素

lt;providergt; content providers元素

您包含在资源中但未在清单中声明的activities,services和content providers对系统不可见,因此永远无法运行。但是,broadcast receivers可以在清单中声明,也可以在代码中动态创建(作为BroadcastReceiver对象),然后通过调用registerReceiver()在系统中注册。

声明组件能力

如上所述,在激活组件中,您可以使用intent启动activities,services和broadcast receivers。您可以通过在intent中显式命名目标组件(使用组件类名称)来实现。但是,intent的真正力量在于intent actions的概念。使用intent actions,您只需描述要执行的动作的类型(以及可选的执

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


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

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

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