创建应用程序和Activity外文翻译资料

 2022-08-22 10:08

3

Creating Applications and

Activities

WHATrsquo;S IN THIS CHAPTER?

Introducing the Android application components and the different

types of applications you can build with them

Understanding the Android application lifecycle

Creating your application manifest

Using external resources to provide dynamic support for locations,

languages, and hardware configurations

Implementing and using your own Application class

Creating new Activities

Understanding an Activityrsquo;s state transitions and lifecycle

To write high-quality applications, itrsquo;s important to understand the components they consist

of and how those components are bound together by the Android manifest. This chapter intro-

duces each of the application components, with special attention paid to Activities.

Next, yoursquo;ll see why and how you should use external resources and the resource hierarchy to

create applications that can be customized and optimized for a variety of devices, countries,

and languages.

In Chapter 2, “Getting Started,” you learned that each Android application runs in a separate

process, in its own instance of the Dalvik virtual machine. In this chapter, you learn more

about the application lifecycle and how the Android run time can manage your application.

You are also introduced to the application and Activity states, state transitions, and event

handlers. The applicationrsquo;s state determines its priority, which, in turn, affects the likelihood

of its being terminated when the system requires more resources.

c03.indd

53

4/11/2012 10:06:14 AM

54 x

CHAPTER 3 CREATING APPLICATIONS AND ACTIVITIES

You should always provide the best possible experience for users, no matter which country theyrsquo;re in

or which of the wide variety of Android device types, form factors, and screen sizes theyrsquo;re using.

In this chapter, you learn how to use the resource framework to provide optimized resources, ensur-

ing your applications run seamlessly on different hardware (particularly different screen resolutions

and pixel densities), in different countries, and supporting multiple languages.

TheActivity class forms the basis for all your user interface (UI) screens. You learn how to create

Activities and gain an understanding of their lifecycles and how they affect the application lifetime

and priority.

Finally, you are introduced to some of theActivity subclasses that simplify resource management

for some common UI patterns, such as map- and list-based Activities.

WHAT MAKES AN ANDROID APPLICATION?

Android applications consist of loosely coupled components, bound by the application manifest

that describes each component and how they interact. The manifest is also used to specify the

applicationrsquo;s metadata, its hardware and platform requirements, external libraries, and required

permissions.

The following components comprise the building blocks for all your Android applications:

Activities — Your applicationrsquo;s presentation layer. The UI of your application is built around

one or more extensions of theActivity class. Activities use Fragments and Views to layout

and display information, and to respond to user actions. Compared to desktop development,

Activities are equivalent to Forms. Yoursquo;ll learn more about Activities later in this chapter.

Services — The invisible workers of your application. Service components run without a UI,

updating your data sources and Activities, triggering Notifications, and broadcasting Intents.

Theyrsquo;re used to perform long running tasks, or those that require no user interaction (such

as network lookups or tasks that need to continue even when your applicationrsquo;s Activities

arenrsquo;t active or visible.) Yoursquo;ll learn more about how to create and use services in Chapter 9,

“Working in the Background.”

Content Providers — Shareable persistent data storage. Content Providers manage and persist

application data and typically interact with SQL databases. Theyrsquo;re also the preferred means

to share data across application boundaries. You can configure your applicationrsquo;s Content

Providers to allow access from other applications, and you can access the Content Providers

exposed by others. Android devices include several native Content Providers that expose

useful databases such as the media store and contacts. Yoursquo;ll learn how to create and use

Content Providers in Chapter 8, “Databases and Content Providers.”

Intents — A powerful interapplication message-passing framework. Intents are used exten-

sively throughout Android. You can use Intents to start and stop Activities and Services, to

broadcast messages system-wide or to an explicit Activity, Service, or Broadcast Receiver, or

to request an action be performed on a particular piece of data. Explicit, implicit, and broad-

cast Intents are explored in more detail in Chapter 5, “Intents and Broadcast Receivers.”

c03.indd

54

4/11/2012 10:06:18 AM

Introducing the Application Manifest File x 55

Broadcast Receivers — Intent listeners. Broadcast Receivers enable your application to listen

for Intents that match the criteria you specify. Broadcast Receivers start your application

to react to any received Intent, making them perfect for creating event-driven applications.

Broadcast Receivers are covered with Intents in Chapter 5.

Widgets — Visual application components that are typically added to the device home screen.

A special variation of a Broadcast Receiver, widgets enable you to create dynamic, interactive

application

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


创建应用程序和Activity

本章内容

bull;介绍Android应用程序组件,以及可用这些组件构建的各种Android应用_

bull; Android应用程序的生命周期

bull;如何创建应用程序Manifest

bull;如何使用外部资源提供对位置、语言和硬件配置的动态支持

bull;如何实现和使用自己的Application类

bull;如何创建新Activity

理解Activity的状态转换和生命周期

在开始编写高质景的Android应用程序之前,需要理解Android应用程序的构成以及各个组件 是如何使用AndroidManifest组合到一起的。本章将介绍Android应用程序的各个组件,并特别关 注 Activity。

然后,本章将讨论为什么以及怎样使用外部资源和资源层次结构来创建可以定制,并针对多种 设备、多个国家和多种语言进行优化的应用程序。

近年来,开发框架有趋向于托管代码的趋势,例如,Java虚拟机和.NET的公共语言运行时。

在第2章中,已经知道了每一个Android应用程序都运行在自己的Dalvik虚拟机实例的独立进 程中。在本章中,你将会学习更多关于应用程序生命周期的内容,以及Android运行时是如何管理 它们的。接着,本章介绍了应用程序和Activity的状态、状态转换和事件处理程序。应用程序的状态决定了它的优先级。在系统需要更多资源的时候,应用程序的优先级的高低将决定它被终止的可 能性的大小。

无论用户在哪个国家,无论他们的Android设备的类型、外形因素和屏幕尺寸是什么样子,都 应该为他们提供尽可能好的体验。在本章中,将学习如何使用资源框架提供优化的资源,确保应用 程序支持多种语言,并能在不同的硬件(特别是不同的屏幕分辨率和像素密度)、不同的国家之中无 差别地运行。

3.1 Android应用程序的组成部分

Android应用程序由松散稱合的组件组成,并使用应用程序Manifest绑定到bull;起:应用程序Manifest 描述了毎bull;个组件和它们之间的交包方式,还用于指定应用程序元数据、其硬件和平台要求、外部库 以及必需的权限。

以下几个组件提供了应用程序的基本结构模块:

  • Activity应用程序的表示层。应用程序中的毎行都是通过Activity类的一个或多个扩 展实现的。Activity使用Fragment和视图来布局和显示信息,以及响应用户动作。在桌面开发环境中,Activity就相当于Form。本章后面的部分将学习更多关于Activity的内容。
  • Service应用程序中不可见的工作者。Service组件在运行时没WU1,它们可以更新数据源 和Activity、触发通知和广播Intent。它们被用来执行一些运行时间长的任务,或者不需要 用户交互的任务(例如,即使当应用程序的Activity不是活动的或者可见的时候也需要继续 进行的网络查找或其他网络任务)。第9章将介绍如何创建和使用Service。
  • Content Providerhellip;个可共享的持久数据存储器。Content Provider用来管理和持久化应用 程序数据,通常会与SQL数据库交互。Content Provider是在应用程序之间共享数据的旨选 方法。可以通过配置自己的Content Provider来允许其他应用程序访问,也可以访问其他应 用程序提供的Content Provider。Android设备包含了多个本地Content Provider来提供打用的 数据库,如媒体库和联系人信息等。在第8章中将学习如何创建和使用Content Provider。
  • Intent —个强大的应用程序间的消息传递框架。Android中大量使用了 Intent。Intent可以 用来妇动和停止Activity和Service,在系统范围内或向目标Activity、Service或Broadcast Receiver广播消息,以及请求对特定的一条数据执行操作。第5章将介绍显式Intern、隐式 Intent 和广播 Intent。
  • Broadcast Receiver Intent侦听器。Broadcast Receiver使应用程序可以监听到那些匹Sd指定 的过滤标准的Intent广播。Broadcast Receiver会自动地启动应用程序来响应某个收到的 Intent,这个特点使它们成为了事件驱动的应用程序的最佳选择。第5章在介绍Intent时会 讨论 Broadcast Receiver。
  • Widget通常添加到设备主屏幕的可视化应用程序组件。Widget是Broadcast Receiver的特 殊变体,可用于创建动态的交互式应用程序组件,用户可以把这些组件添加到他们的主屏 幕匕。第14章将讨论如何创建自己的Widget。
  • Notification Notification允许向用户发送信号,但却不会过分吸引他们的注意力或者打断 他们当前的Activity。它们是应用程序不可见或者不活动时(特别是Service或者Broadcast Receiver)吸引用户注意的首选方法。例如,当设备接收到-个文本消息或者电子邮件的时候, 消息传递应用程序或者Gmail应用程序可以通过闪灯、发出声音、显示图标或者滚动显示通过分离这些应用程序组件之间的依赖性,可以和其他应用程序(不论是你自己的应用程序,还 是其他第三方的应用程序)

共享和使用单独的Content Provider、Service甚至Activity消息摘要的方式来提醒你,这就是利用了 Notification功能。也可以在自己的应用程序中使 用Notification。

3.2应用程序Manifest文件简介

每个Android项目都包含hellip;个Manifest文件 Android Manifestxml,它存储在项目层次中的

最底层。Manifest可以定义应用程序及其组件和需求的结构和元数据。

它包含了组成应用程序的每一个 Activity、Service、Content Provider 和 Broadcast Receiver 的节 点,并使用Intent Filter和权限来确定这些组件之间以及这些组件和其他应用程序是如何交互的。

Manifest文件还可以指定应用程序的元数据(如它的图标、版本号或者主题)以及额外的顶层节 点,这些节点可用来指定必需的安全权限和单元测试,以及定义硬件、屏幕和平台支持要求,如下 所述。

Manifest文件由多个根manifest标签构成,该标签带有1个被设为项目包的package属性。它通 常包含多个xmlns:android厲性来提供文件内使用的某些系统属性。

使用versionCode属性可将当前的应用程序版本定义为一个整数,每次版本迭代时,这个数字 都会增加。使用versionName可定义一个显示给用户的公共版本号。

通过使用installLocation属性,还可以指定是否允许(或者首选)将应用程序安装到外部存储器(通 常是SD卡)而不是内部存储器上。为此,可以将其值指定为preferExtemal或auto,使用前者时,只 要有可能就会把应用程序安装到外部存储器上,后者则要求系统决定。

-当把应用程序安装到外部存储器上时,如果用户使用USB大容量存储器向计算机 复制文件,或者从计算机复制文件,或者如果用户拒绝或者取出SD卡,应用程序将 立即终止.

如果不指定installLocation M性,应用程序将安装到内部存储器,而用户将无法把应用程序移动 到外部存储器。内部存储器的容景hellip;般是有限的,所以最好是只要W可能,就把应用程序安装到外 部存储器。

由于取出或者拒绝外部存储器存在的问题,安装到外部存储器对某些应用程序来说并不适合, 这些应用程序包括:

bull;具有 Widget、Live Wallpaper 和 Live Folder 的应用程序 Widget、Live Wallpaper 和 Live Folder将从主屏幕上移除,而且在重启系统前可能不再可用。

bull;提供不中断服务的应用程序应用程序和它运行的服务将被停止,并且不会重新启动。

bull;输入法引擎(Input Method Engine,IME)安装到外部存储器的任何IME都会被禁用。在 外部存储器再次可用后,用户必须重新选择IME。

bull;设备管理器DeviceAdminReceiver及其管理能力将被禁用。

3.2.1应用程序Manifest文件详解

下面的XML代码段展示了一个典型的Manifest节点:

lt;manifest xmlns:android='http://schemas.android.com/apk/res/android'
pac kage=*bull; com bull; paad. myapp '
android:vers ionCode= M1'
android:versionName='0.9 Beta'
android: ins tallLocation*,,preferExternalMgt;

[...manifest nodes ...]

lt;/manifestgt;

manifest标签包含了一些节点(node),它们定义了组成应用程序的应用程序组件、安全设置、测
试类和需求。下面列出了一些manifest子节点标签,并用一些XML代码段说明了它们是如何使用的。

bull; uses-sdk这个节点用于定义要想正确地运行应用程序,设备上必须具有的最低和最高
SDK版本,以及为应用程序设计的目标SDK,这分别通过使用minSDKVersion、
maxSDKVersion 和 targetSDKVersion 属性设置。

最低SDK:版本指定了包含应用程序中使用的API的最低SDK版本。如果没有指定最低SDK 版本,其默认值为1,在这种情况下,如果应用程序试图调用不可用的API,那么就会失败。 最高SDK版本用于定义想要支持的最高SDK版本。在Android Market上列出的对运行更 商平台版本的设备可用的应用程序中,你的应用程序不会显示。最好不要设置最高SDK版 本,除非你知道应用程序在更新的平台版本上肯定不能正确工作。

目标SDK版本属性用于指定你在开发和测试应用程序时使用的平台。设置目标SDK版本会 告诉系统不需要为支持该版本而进行任何前向和后向兼容性更改。为了利用最新的平台U1 改进,当确认应用程序在最新的平台版本上的表现符合预期后,即使应用程序中没有使用 任何新的API,也应该将其目标SDK设为最新的平台版本,这被认为是一种最佳实践。

通常,没有必要指定最高SDK版本,也很少有人支持那么做。最高SDK版本用于定义想 要支持的最高SDK版本。在运行更高平台版本的设备的Android Play Store上,你的应用程序不会显示。运行的平台版本高于Android 2.0.1(API level 6)的设备在安装时将忽略任何M 高SDK值。

lt;uses-sdk android:minSdkVersion*'6'

android:targetSdkVersion='15'/gt;

支持的SDK版本不等同于平台版本,也不能从平台版本导出.例如,Android平台的版本4.0支持SDK版本14.要想找出与每个平台对应的SDK版本,可以使用这个网址的资源:
http/developer.android.com/guide/qgt;pendix/ levels, html.

bull; uses-configuration使用uses-configuration节点可以指定应用程序支持的每个输入机制的

组合。一般不需要包含这个节点,不过对于需要特殊输入控制的游戏来说,它是很有用的。 可以指定以下输入设备的任意组合:

reqFiveWayNav如果要求输入设备能够向上、向下、向左和向右导航,并且能够单击 当前的选项,那么需要将这个属性指定为true。这包括跟踪球和D-padf任承 reqHardKeyboard如果应用程序需要硬件键盘,则将此属性指定为true。 reqKeyboardtype 用于将键盘类型指定为 nokeys、qwerty、twelvekey 或 undefined 中 的一种。

reqNavigation 将属性值指定为 nonav、dpad、trackball、wheel或 undefined 其中之一, 作为必需的导航设备。

reqTouchScreen 选择 notouch、stylus、finger 或 undefined 其中之一,以指定必需的触摸屏输入

可以指定多个支持的配置,例如,指定设备具有触摸屏、跟踪球以及一个QUERTY82键硬件键盘,如下所示:

lt;uses-configuration android:reqTouchScreen='fingerM

android:reqNavigation='trackballM lt;

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


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

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

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