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

 2021-11-23 10:11

Android应用程序基础知识

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

  • 定义应用程序的核心框架组件。
  • 清单文件,用于为应用程序声明组件和所需的设备功能。
  • 独立于应用程序代码的资源,允许您的应用程序针对各种设备配置优雅地优化其行为。

应用程序组件

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

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

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

活动

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

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

服务

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

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

内容提供商

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

内容提供程序对于读取和写入应用程序专用但不共享的数据也很有用。例如,Note Pad示例应用程序使用内容提供程序来保存注释。

内容提供程序是作为ContentProvider的子类实现的,并且必须实现一组标准API,以使其他应用程序能够执行事务。有关更多信息,请参阅Content Providers开发人员指南。

广播接收器

广播接收器是响应系统范围广播公告的组件。 许多广播来自系统 - 例如,宣布屏幕已关闭,电池电量低或拍摄照片的广播。 应用程序还可以启动广播 - 例如,让其他应用程序知道某些数据已下载到设备并可供他们使用。 虽然广播接收器不显示用户界面,但是它们可以创建状态栏通知以在广播事件发生时警告用户。 但更常见的是,广播接收器只是其他组件的“网关”,旨在完成非常少量的工作。 例如,它可能会启动服务以根据事件执行某些工作。

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

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

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

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

激活组件

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

使用Intent对象创建intent,该Intent对象定义用于激活特定组件或特定类型组件的消息 - intent可以分别是显式的或隐式的。

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

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

另一个组件类型,内容提供程序,不会被意图激活。相反,它是在ContentResolver请求的目标下激活的。内容解析程序处理与内容提供程序的所有直接事务,以便与提供程序执行事务的组件不需要而是调用ContentResolver对象上的方法。这在内容提供者和请求信息的组件之间留下了一层抽象(为了安全起见)。

有各种方法可以激活每种类型的组件:

  • 您可以通过将Intent传递给startActivity()或startActivityForResult()(当您希望活动返回结果时)来启动活动(或给它做新的事情)。
  • 您可以通过将Intent传递给startService()来启动服务(或向正在进行的服务发出新指令)。 或者您可以通过传递Intent tobindService()来绑定到服务。
  • 您可以通过将Intent传递给sendBroadcast(),sendOrderedBroadcast()或sendStickyBroadcast()等方法来发起广播。
  • 您可以通过在ContentResolver上调用query()来对内容提供程序执行查询。

有关使用意图的更多信息,请参阅意图和意图过滤器文档。 以下文档还提供了有关激活特定组件的更多信息:活动,服务,BroadcastReceiver和内容提供商。

声明组件

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

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;元素
  • 服务的lt;servicegt;元素
  • 广播接收器的lt;receivergt;元素
  • 内容提供商的lt;providergt;元素

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

声明组件功能

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

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

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

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

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

声明应用程序要求

Android提供各种设备,但并非所有设备都提供相同的功能。为了防止您的应用程序安装在缺少应用程序所需功能的设备上,通过在清单文件中声明设备和软件要求,明确定义应用程序支持的设备类型的配置文件非常重要。这些声明中的大多数仅供参考,系统不会读取它们,但Google Play等外部服务会读取它们,以便在用户从其设备搜索应用程序时为其提供过滤。

例如,如果您的应用程序需要摄像头并使用Android 2.1(API级别7)中引入的API,则应将这些API声明为清单文件中的要求。这

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

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

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