登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 电子信息类 > 电子信息工程 > 正文

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

 2022-08-11 02: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—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:

  • 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

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


安卓应用基础

Android应用程序是使用Java编程语言编写的。 Android SDK工具将代码,连同所有数据和资源文件一起编译为后缀为apk的Android程序包。一个apk文件中的所有代码被视为一个应用程序,并且是Android驱动的设备用来安装该应用程序的文件。

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

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

 ●默认情况下,系统为每个应用程序分配一个唯一的Linux用户ID(该ID仅由系统使用,应用程序未知)。系统为应用程序中的所有文件设置权限,只有得到该应用程序用户ID才能访问它们。

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

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

通过这种方式,Android系统实现了最小特权的原则。也就是说,默认情况下,每个应用程序只能访问执行其工作所需的组件,而不能访问其他组件。这将创建一个非常安全的环境,在该环境中,应用程序无法访问未获得其权限的部分。

但是,应用程序可以通过以下方式与其他应用程序共享数据,以及应用程序访问系统服务:

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

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

这涵盖了有关Android应用程序在系统中如何存在的基础知识。本文档的其余部分向你介绍:

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

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

●与应用程序代码分开的资源,可让你的应用程序针对各种设备配置适当地优化其行为。

应用组件

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

有四种不同类型的应用程序组件。每种类型都有不同的用途,并具有不同的生命周期,生命周期定义了组件的产生与销毁。

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

Activities

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

活动作为一个activity的子类实现,你可以在activity的开发指南中了解到更多。

Services

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

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

Content providers

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

在写入和读取那些,对应用程序来说是私有而不是共享的数据时,内容提供者也很有用,例如,记事本示例应用程序使用内容提供者来保存笔记。

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

Broadcast receivers

广播接收机是响应全系统广播通知的组件。许多广播都来自系统,例如,广播宣布屏幕关闭,电池电量不足或拍摄了图片。应用程序还可以启动广播,例如,让其他应用程序知道某些数据已下载到设备并可供其使用。尽管广播接收器不显示用户界面,但它们可能会创建状态栏通知来在发生广播事件时提醒用户。不过,更常见的是,广播接收器只是通向其他组件的“门户”,意在进行很少量的工作。例如,它可以启动服务以基于事件执行一些工作。

广播接收器作为BroadcastReceiver的子类来实现,每个广播作为Intent对象来传递。了解更多信息,请参见该BroadcastReceiver类。

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

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

由于系统在单独的进程中运行每个应用程序,并且文件权限限制对其他应用程序的访问,因此应用程序无法直接从其他应用程序激活组件。但是,Android系统可以。因此,要在另一个应用程序中激活组件,你必须向系统传递一条消息,指定你打算启动特定组件的意图。然后,系统为你激活该组件。

激活组件

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

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

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

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

另一个组件类型,内容提供者,不会被Intent激活。而是当ContentResolver的请求作为目标时将其激活。contentresolver处理与内容提供者的所有直接交易,因此与提供者进行交易的组件不需要,而是采用调用ContentResolver对象的方法。这就在内容提供者和请求信息的组件之间保留了一层抽象(出于安全性考虑)。

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

bull;你可以通过传递一个Intent到startActivity()或startActivityForResult()来开始一个活动或赋予它一些新的东西做(当你要查看的活动返回结果)。

bull;你可以通过传递一个Intent到startService()来可以启动一个服务(或给予一个持续服务的新指令)或者,你可以通过传递Intent到bindService()来绑定一个服务 。

bull;你可通过使传递Intent到sendBroadcast() ,sendOrderedBroadcast() ,或sendStickyBroadcast()来发起一个广播 。

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

有关使用意图的更多信息,请参见“ Intents and Intent Filters”文档。以下文档中还提供了有关激活特定组件的更多信息:活动,服务,广播接收器和内容提供者。

声明组件

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

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()在系统中注册。

声明组件功能

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

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

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

例如,具有编写新电子邮件活动的电子邮件应用程序可能会在清单中声明一个Intent filters,以申请基础响应“发送”Intent(以便发送电子邮件)。然后,应用程序中的活动可以使用“发送”操作(ACTION_SEND)创建一个意图,系统将其与电子邮件应用程序的“发送”活动进行匹配,并在你使用startActivity()调用该意图时将其启动。

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

声明申请要求

Android提供了多种

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


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

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

企业微信

Copyright © 2010-2022 毕业论文网 站点地图