Android外文文献翻译
发布时间:2024-09-20
发布时间:2024-09-20
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'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's components need to be executed, then shuts down the process when it'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's possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each other'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'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:
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.
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'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
共 21 页 第 1 页
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's contact information. As such, any application with the proper permissions can query part of the content provider (such as ContactsContract.Data) to read and write information about a particular person.
Content providers are also useful for reading and writing data that is private to your application and not shared. For example, the Note Pad sample application uses a content provider to save notes.
A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other applications to perform transactions. For more information, see the Content Providers developer guide.
Broadcast receivers
A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
共 21 页 第 2 页
A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.
A unique aspect of the Android system design is that any application can start another
application’s component. For example, if you want the user to capture a photo with the device camera, there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. You don't need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo. When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application.
When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component. For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process.
Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main() function, for example).
Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application. The Android system, however, can. So, to activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.
Activating Components
Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another.
An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.
共 21 页 第 3 页
For activities and services, an intent defines the action to perform (for example, to "view" or "send" something) and may specify the URI of the data to act on (among other things that the component being started might need to know). For example, an intent might convey a
request for an activity to show an image or to open a web page. In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an Intent (for example, you can issue an intent to let the user pick a personal contact and have it returned to you—the return intent includes a URI pointing to the chosen contact).
For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates "battery is low").
The other component type, content provider, is not activated by intents. Rather, it is
activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that's performing
transactions with the provider doesn't need to and instead calls methods on
the ContentResolver object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
There are separate methods for activating each type of component:
You can start an activity (or give it something new to do) by passing
an Intent to startActivity() or startActivityForResult() (when you want the activity to return a result).
You can start a service (or give new instructions to an ongoing service) by passing
an Intent to startService(). Or you can bind to the service by passing an Intent tobindService().
You can initiate a broadcast by passing an Intent to methods
like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
You can perform a query to a content provider by calling query() on a ContentResolver.
For more information about using intents, see the Intents and Intent Filters document. More information about activating specific components is also provided in the following
documents: Activities, Services, BroadcastReceiver and Content Providers.
Declaring components
共 21 页 第 4 页
The primary task of the manifest is to inform the system about the application's
components. For example, a manifest file can declare an activity as follows:
In the <application> element, the android:icon attribute points to resources for an icon that identifies the application.
In the <activity> element, the android:name attribute specifies the fully qualified class name of the Activity subclass and the android:label attributes specifies a string to use as the user-visible label for the activity.
You must declare all application components this way:
1、<activity> elements for activities
2、<service> elements for services
3、<receiver> elements for broadcast receivers
4、<provider> elements for content providers
Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run.
However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver objects) and registered with the system by
calling registerReceiver().
Declaring component capabilities
共 21 页 第 5 页
As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers. You can do so by explicitly naming the target component (using the component class name) in the intent. However, the real power of intents lies in the concept of intent actions. With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which you’d like to perform the action) and allow the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use.
The way the system identifies the components that can respond to an intent is by
comparing the intent received to the intent filters provided in the manifest file of other applications on the device.
When you declare a component in your application's manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. You can declare an intent filter for your component by adding
an <intent-filter> element as a child of the component's declaration element.
For example, an email application with an activity for composing a new email might declare an intent filter in its manifest entry to respond to "send" intents (in order to send email). An activity in your application can then create an intent with the “send” action
(ACTION_SEND), which the system matches to the email application’s “send” activity and launches it when you invoke the intent with startActivity().
For more about creating intent filters, see the Intents and Intent Filters document.
Declaring application requirements
There are a variety of devices powered by Android and not all of them provide the same features and capabilities. In order to prevent your application from being installed on devices that lack features needed by your application, it's important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file. Most of these declarations are informational only and the system does not read them, but external services such as Google Play do read them in order to provide filtering for users when they search for applications from their device.
共 21 页 第 6 页
For example, if your application requires a camera and uses APIs introduced in Android
2.1 (API Level 7), you should declare these as requirements in your manifest file. That way, devices that do not have a camera and have an Android version lower than 2.1 cannot install your application from Google Play.
However, you can also declare that your application uses the camera, but does
not require it. In that case, your application must perform a check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available. Here are some of the important device characteristics that you should consider as you design and develop your application:
Screen size and density
In order to categorize devices by their screen type, Android defines two characteristics for each device: screen size (the physical dimensions of the screen) and screen density (the physical density of the pixels on the screen, or dpi—dots per inch). To simplify all the different types of screen configurations, the Android system generalizes them into select groups that make them easier to target.
The screen sizes are: small, normal, large, and extra large.
The screen densities are: low density, medium density, high density, and extra high density.
By default, your application is compatible with all screen sizes and densities, because the Android system makes the appropriate adjustments to your UI layout and image resources. However, you should create specialized layouts for certain screen sizes and provide
specialized images for certain densities, using alternative layout resources, and by declaring in your manifest exactly which screen sizes your application supports with
the <supports-screens> element.
For more information, see the Supporting Multiple Screens document.
Input configurations
Many devices provide a different type of user input mechanism, such as a hardware keyboard, a trackball, or a five-way navigation pad. If your application requires a particular kind of input hardware, then you should declare it in your manifest with the <uses-configuration> element. However, it is rare that an application should require a
共 21 页 第 7 页
certain input configuration.
Device features
There are many hardware and software features that may or may not exist on a given Android-powered device, such as a camera, a light sensor, bluetooth, a certain version of OpenGL, or the fidelity of the touchscreen. You should never assume that a certain feature is available on all Android-powered devices (other than the availability of the standard Android library), so you should declare any features used by your application with the <uses-feature> element.
Platform Version
Different Android-powered devices often run different versions of the Android platform, such as Android 1.6 or Android 2.3. Each successive version often includes additional APIs not available in the previous version. In order to indicate which set of APIs are available, each platform version specifies an API Level (for example, Android 1.0 is API Level 1 and Android
2.3 is API Level 9). If you use any APIs that were added to the platform after version 1.0, you should declare the minimum API Level in which those APIs were introduced using the <uses-sdk> element.
It's important that you declare all such requirements for your application, because, when you distribute your application on Google Play, the store uses these declarations to filter
which applications are available on each device. As such, your application should be available only to devices that meet all your application requirements.
For more information about how Google Play filters applications based on these (and other) requirements, see the Filters on Google Play document.
Application Resources
An Android application is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the application. For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files. Using application resources makes it easy to update various characteristics of your application without
modifying code and—by providing sets of alternative resources—enables you to optimize
共 21 页 第 8 页
your application for a variety of device configurations (such as different languages and screen sizes).
For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your application code or from other resources defined in XML. For example, if your application contains an image file named logo.png (saved in the res/drawable/ directory), the SDK tools generate a resource ID named R.drawable.logo, which you can use to reference the image and insert it in your user interface.
One of the most important aspects of providing resources separate from your source code is the ability for you to provide alternative resources for different device configurations. For example, by defining UI strings in XML, you can translate the strings into other languages and save those strings in separate files. Then, based on a language qualifier that you append to the resource directory's name (such as res/values-fr/ for French string values) and the user's language setting, the Android system applies the appropriate language strings to your UI.
Android supports many different qualifiers for your alternative resources. The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used. As another example, you should often create different layouts for your activities, depending on the device's screen
orientation and size. For example, when the device screen is in portrait orientation (tall), you might want a layout with buttons to be vertical, but when the screen is in landscape orientation (wide), the buttons should be aligned horizontally. To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout's directory name. Then, the system automatically applies the appropriate layout
depending on the current device orientation.
For more about the different kinds of resources you can include in your application and how to create alternative resources for various device configurations, see theApplication Resources developer guide.
安卓应用基础
共 21 页 第 9 页
在Java编程语言编写的Android应用程序的Android的SDK工具编译代码以及与任何数据和到一个Android的包,一个归档文件档案资源的.apk后缀,所有的在一个单一的代码.apk文件被认为是一个应用程序,是Android的文件,供电设备来安装应用程序。 一旦安装在设备上,每个Android应用程序的生命在它自己的安全沙箱:
而Android操作系统是一个多用户Linux系统中,每个应用程序是一个不同的用户。 默认情况下,每个应用程序的系统分配一个唯一的Linux用户ID(该ID仅用于由系统是未知的应用程序),系统设置所有的应用程序中的文件权限,以便只有用户ID分配给该应用程序可以访问它们。
每个进程都有它自己的虚拟机(VM),因此应用程序的代码在从其他应用程序隔离运行。
默认情况下,每个应用程序运行在它自己的Linux进程。Android的启动过程时,应用程序的任何组件需要被执行,然后关闭该进程时,它不再需要或恢复时,系统必须为其他应用程序的内存。
这样一来,Android系统实现了最小特权原则,也就是说,每个应用程序,默认情况下,只能访问的组件,它需要做的工作,没有更多,这将创建一个非常安全的环境,使应用程序无法访问的,这就是它没有给予许可制度的部分。
但是,有一个应用程序的方法与其他应用程序和应用程序访问系统服务的数据:
这有可能为两个应用程序安排共享相同的Linux用户ID,在这种情况下,它们能够相互访问的文件。为了节约使用相同的用户ID系统资源,应用程序还可以安排运行在相同的Linux进程和共享同一个VM(应用也必须使用相同的证书签名)。
应用程序可以请求访问权限,如用户的联系人,短信,可安装存储(SD卡),摄像头,蓝牙等设备的数据,所有应用程序的权限必须由用户在安装时授予。
这涵盖了基本就如何Android应用程序在系统中存在这个文件的其余部分向您介绍:
1、框架的核心组件定义应用程序。
2、清单文件中声明组件和应用程序所需的设备功能。
3、资源是从应用程序代码分开,并允许您的应用程序正常优化的设备配置各种其行为。
Android的核心功能之一就是一个应用程序可以使用其它应用程序的元素(如果那个应用程序允许的话)。比如说,如果你的应用程序需要一个图片卷动列 表,而另一个应用程序已经开发了一个合用的而又允许别人使用的话,你可以直接调用那个卷动列表来完成工作,而不用自己再开发一个。你的应用程序并没有吸纳 或链接其它应用程序的代码,它只是在有需求的时候启动了其它应用程序的那个功能部分。
为达到这个目的,系统必须在一个应用程序的一部分被需要时启动这个应用程序,并将那个部分的Java对象实例化。与在其它系统上的应用程序不同,Android应用程序没有为应用准备一个单独的程序入口(比如说,没有main()方法), 而是为系统依照需求实例化提供了基本的组件。共有四种组件类型:
活动(Activities)
一个 activity 代表用户界面的一个独立屏幕。例如,一个邮件应用程序应该有一个 activity 用于显示新邮件列表,另一个 activity 用于撰写一封邮件,还有一个 activity 用于读取邮件。尽管所有 activitie 协同工作以构成邮件应用程序的用户体验,但彼此之间相对独立。应次,不同的应用程序能够从任何一个 activity 启动 (只要邮件应用程序允许)。例如,用户需要分享一张照片,一个拍照应用程序能够启动邮件应用程序的 activity 。
activity 是一个实现了 Activity 的子类,你可以在 Activities 开发者指导部分了解更多。
服务(Services)
service 是在后台运行,执行长时间操作或者执行远程操作。 service 不提供用户界面。例如,当用户在另一个应用程序时,一个 service 可在后台播放音乐,或者是从网络上获取数据,而不阻断用户与当前 activity 的交互。其他组件,比如一个 activity ,为了与该 service 互动,可以启动或者 绑定它。
service 是一个实现了 Service 的子类,你可以在 Services 开发者指导部分了解更多。 广播接收器(Broadcast receivers)
共 21 页 第 11 页
广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的──比如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以进行广播──比如说,通知其它应用程序一些数据下载完成并处于可用状态。
应用程序可以拥有任意数量的广播接收器以对所有它感兴趣的通知信息予以响应。所有的接收器均继承自BroadcastReceiver基类。
广播接收器没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者用NotificationManager来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。
内容提供者(Content providers)
内容提供者将一些特定的应用程序数据供给其它应用程序使用。数据可以存储于文件系统、SQLite数据库或其它方式。内容提供者继承于ContentProvider 基类,为其它应用程序取用和存储它管理的数据实现了一套标准方法。然而,应用程序并不直接调用这些方法,而是使用一个ContentResolver 对象,调用它的方法作为替代。ContentResolver可以与任意内容提供者进行会话,与其合作来对所有相关交互通讯进行管理。
参阅独立的内容提供者Content Providers 章节获得更多关于使用内容提供者的内
容。
每当出现一个需要被特定组件处理的请求时,Android会确保那个组件的应用程序进程处于运行状态,或在必要的时候启动它。并确保那个相应组件的实例的存在,必要时会创建那个实例。
Android系统设计的一个独特方面是任何的一个程序都可以启动另一程序的组件。比如,你想让你的程序可以使用照相机拍照,如果已经有了实现这种功能的程序并且你你的程序能使用它(有权限),那么你就没有再要再写一个新的Activity来实现这个功能。你的程序不需要包含或者链接这个拍照程序。相反,你只需要在你的程序中打开这个拍照程序中的实现拍照功能的Activity。当拍完之后,拍好的照片甚至会自动返回给
共 21 页 第 12 页
你的程序。者对于用户来说,就好像是想拍照功能的程序就是你的这个程序的一部分一样。
当系统启动一个组件之后,如果这个组件所在的程序之前没有运行的话,系统会自动开始这个程序的进程,并初始化这个组件所需要的相关类。比如,你的程序开启了一个拍照功能程序的Activity,这时系统会启动这个Activity所在的程序,所以这个Activity运行在拍照功能的程序当中,而不是在你的程序中。所以,不像其他操作系统的中的程序一样,Android程序没有一个单独的入口点(比如没有我们常见的main()函数)。 因为系统中的程序运行在自己的独立进程中,并且程序中的文件都有自己的限制其他程序访问的权限,所以,你的程序不能直接激活其他程序中的组件。但是Android系统就可以。具体是这样的实现的,为了激活(activate)其他程序中的组件,你必须向系统发送一个消息来详细说明你要启动其他组件的意图,这样系统才会为你激活这个组件。
激活组件(Activating Components)
四大组件中的三个组件——activities、services和broadcast receiver——是由一种叫intent的异步消息来激活的。这些intents在运行时(runtime)将这些属于你的程序或不同程序的单独的组件绑定在一起(bind),你可以把这些intents看作是需要其他组件的action的messengers。
一个intent就是一个Intent对象,这个intent定义了一种可以激活(activate)某个特定组件或者某种特定类型的组件,这两种情况分别对应两种intent的定义方式或者显示的或者隐式的。
对于activities和services,一个intent定义了要执行的操作(action)(比如,要“view”或者“send”什么)和要操作的数据的URI。比如,一个intent可能会为一个activity传递一个请求来展示一张图片或者打开一个网页。有时,你可以启动一个activity来得到返回的结果,在这个例子中这个activity的返回的结果也是一个Intent(比如,你可以发送一个intent让用户选择一个personal contact并返回给你——这个返回的intent就包含了一个指向用户选择的联系人的URI)。(关于activity和service的启动方式,下面将介绍。)
共 21 页 第 13 页
对于广播接收者来说,intent只是简单的定义了要广播的内容(比如,一个用以表明电池电量很低的广播仅包含了一个表明电池电量很低的字符串)。
最后一种组件类型content provider并不是由intent来激活的(activate)。而是由接收到ContentResolver的请求时激活的。
它们都各自有自己的方法来激活相应的组件:
你可以通过传递一个Intent给startActivity()或startActivityForResult()启动一个
activity(或者给他一些新的要做的内容)。使用startActivityForResult()你将得到一个返回结果。
你可以通过传递一个Intent给startService()来start一个service(或者给一个正在运行的service一些新的指令(instructions))。或者你可以通过把一个Intent传递给
bindService()来绑定一个service。
你可以通过传递一个Intent给诸如sendBroadcast()、sendOrderedBroadcast()或者
sendStickyBroadcast()等方法来初始化一个广播。
你可以通过调用ContentResolver的query()方法来执行一次content provider的查询操作。
更多的关于intent的内容,可以参看文档中的Intents and Intent Filters。更多的关于激活特定组件的内容可以参看文档中的:Activities、Services、BroadcastReceiver、Content Providers。
关于Manifest文件
在Android系统可以启动一个应用程序组件之前,Android系统必须通过读取这个程序的AndroidManifest.xml(即manifest文件)文件来确定要启动的组件存在。你的程序必须在这个manifest文件声明用到的所有的组件,并且这个manifest文件必须在项目的根目录下。
另外,这个manifest文件还声明一些其他的东西,比如:
确定这个程序需要的所有权限,比如Internet访问权限或者读取用户联系人权限。
共 21 页 第 14 页
声明这个运行这个程序所需要的最低API版本,这个可以根据开发该程序所使用的API版本。
声明该程序所需要的硬件或软件特征(features),比如照相机、蓝牙服务或者多点触屏。
声明该程序需要链接(link against)的API库(不是Andorid的framework APIs),比如Google Maps library。
组件声明
Manifest文件的首要任务就是通知系统关于程序中要使用的组件。比如,一个manifest文件可以用如下的方式来声明一个activity:
[java] view plaincopy
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest ... >
3. <application android:icon="@drawable/app_icon.png" ... >
4. <activity android:name="com.example.project.ExampleActivity"
5. android:label="@string/example_label" ... >
6. </activity>
7. ...
8. </application>
9. </manifest>
在<application>元素中,android:icon属性用于指定一个用于标示该程序的icon。 在<activity>元素中,android:name属性用于确定这个扩展自Activity的子类的全路径名,android:label属性用于标示这个activity的对于用户可见的label。
你必须要用以下方式来声明你的程序组件:
1、 activities:<activity>标签
2、 services:<service>标签
3、 broadcast receiver:<receiver>标签
4、 content providers:<provider>标签
共 21 页 第 15 页
上一篇:拥抱美好未来课件
下一篇:英语泛读3答案__王守仁