【Android Dev Guide - 02】 - 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 anAndroid package, an archive file with an.apksuffix. All the code in a single.apkfile is considered to be one application and is the file that Android-powered devices use to install the application.
Android应用是用Java语言写的,所有的Java代码和相关的资源文件被Android SDK打成一个后缀为apk的Android包。一个APK包里所有的代码都被看做一个程序,并且用来安装应用程序到移动设备上。

Once installed on a device, each Android application lives in its own security sandbox.
一旦安装到设备上,每一个Android应用程序都在自己的世界(虚拟机)里单独存活。


  • The Android operating system is a multi-user Linux system in which each application is a different user.
    Android操作系统是一个多用户的Linux系统,每个程序都是一个单独的用户。
  • 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.
    默认情况下,每个应用程序都会分到一个特殊的Linux用户ID(这个ID只是被系统使用,应用程序并不知道),系统为所有文件设置权限,只有那些有使用权限的程序才可以访问到那些相应的文件。
  • 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 application。
    默认情况下,每一个进程都跑在他自己的Linux进程下,应用程序的任何一段代码需要执行时,Android都会启动这个进程,并且会在应用不需要,或者当系统需要为其他进程恢复内存的时候时候关闭这个进程。

In this way, the Android system implements theprinciple 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.
用这种方法,Android系统实现了principle of least privilege(最少权限原则),默认的,程序只能访问他需要使用到的部分,这样创建了一个很安全的机制,使得程序不能访问系统没有给予权限的部分

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).
    可以安排两个程序共享一个UserID,这样他们可以互相看到对方的文件,共享系统资源,并且在同一个Linux线程里运行,共享虚拟机。
  • 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.
    一个程序可以获取权限访问设备的数据,例如用户的通讯录,短信,扩充存储,摄像,蓝牙和其他。所有的应用应用许可都必须被用户在安装的时候给予授权

    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.
    程序的组件是一个Android应用程序的必备的。每一个组件是不同的角度,系统可以加入到你进入你的程序。不是所有的组件都有用户的实际切入点,其中一些是取决于彼此的,但是每一个都存在他自己的实例并且发挥了一个特殊的角色,每一个组件都是一个定义你程序总体行为的不可获取的一块。

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.
有4种不同类别的程序组件,每一个类型的组件都有不同的目的并且有着不同的生命周期,那定义了组件是如何被创建与销毁的。

Activities
Anactivityrepresents 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 ofActivityand you can learn more about it in theActivitiesdeveloper guide.
Activity提供了一个用户接口的单一界面,例如:一个Email程序可以提供一个activity用来显示一列新邮件,另一个activity可以写一封新邮件,同时还有一个activity用来读取邮件。尽管这些activity共同工作在一个email应用中形成一个有结合力的用户体验,每一个都独立与另外一个。同样的,不同的应用可以启动任何一个activity。例如,一个摄像程序可以启动一个activity在email程序中用来编写一封邮件,以便为了用户分享一张照片。

Services
Aserviceis 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.
Service是一个在后台不间断运作的组件或者为远程进程进行工作的组件。service没有一个用户界面。例如:但用户处理其他事情时,一个service可以背景播放音乐、后台读取网络数据或者处理其他的东西并且提供给其他的activity。另一个组件,类似与activity,可以启动服务并且让他开始运作或者绑定他为了和他进行交互。

Broadcast receivers
Abroadcast receiveris 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 maycreate a status bar notificationto 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.

Broadcast receiver这个组件响应Broadcast广播通知。很多广播来源系统,例如通知屏幕已经锁住、低电量、图片被选中、此时用户开始广播,应用程序也可以初始化广播,例如,让其他应用程序知道一些数据已经开始下载到设备上并且可以为他们使用,尽管广播接受者没有显示用户界面,他们可以在广播事件发生时创建一个状态栏通知来警示用户。更通常的是,尽管广播接受者仅仅是一个通道来通知其他组件并且做了非常少量的工作。例如,他可能基于一些事件来初始化某些服务

Content providers 内容提供

Content providers为其他程序提供数据集,这些数据可以保存在系统文件里或者sqlite数据库里,Content providers继承自 ContentProvider基类,实现了一些标准的方法,可以 让程序检索或者改写其中的数据。程序不能直接的调用那些方法。ContentResolver不能与content provider直接通信,但可以用过ContentResolver 。ContentResolver 可以跟任何的content provider通信,可以与provider合作管理通 信进程。

更多信息查看Content Providers文档。

当有需要特定的请求需要被处理时,Android都会确保他正在运行,需要时会创建一个实例。

Activating Components

Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called anintent. 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.
当ContentResolver接收到一个请求时,运行content providers,其他三个组件activities、services、broadcast receiver被Intents异步消息启动。

An intent is created with anIntentobject, which defines a message to activate either a specific component or a specifictypeof component—an intent can be either explicit or implicit, respectively.

一个Intent可以被Intent对象创建,它定义了一条消息来激活一个指定的组件或者一类特定的组件,一个Intent可以分别显示或者隐式指定。
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 anIntent(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)。
Intent可以传递给组件信息,比如显示一张图片或者打开一个网页,在一些情况下,你可以开启一个activity来接受结果,并且这个activity可以返回结果到Intent中。
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")
对于广播接受者,Intent简单的定义了一个通知。

The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from aContentResolver. 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 theContentResolverobject. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
对于其他组件类型,内容提供者,不是被Intent所激活,而是被目标contentResolver所激活。

  • You can start an activity (or give it something new to do) by passing anIntenttostartActivity()orstartActivityForResult()(when you want the activity to return a result).
    你可以通过startActivity或者startActivtiyForRespult来启动一个activity
  • You can start a service (or give new instructions to an ongoing service) by passing anIntenttostartService(). Or you can bind to the service by passing anIntenttobindService().
    你可以启动一个service,通过Intent到startService,或者你可以绑定一个服务通过bindService
  • You can initiate a broadcast by passing anIntentto methods likesendBroadcast(),sendOrderedBroadcast(), orsendStickyBroadcast().
    你可以初始化一个broadcast通过传一个sendBroadcast(),sendOrderedBroadcast(), orsendStickyBroadcast().
  • You can perform a query to a content provider by callingquery()on aContentResolver
    你可以执行一个查询通过调用query()on aContentResolver


    The Manifest File

    Before the Android system can start an application component, the system must know that the component exists by reading the application'sAndroidManifest.xmlfile (the "manifest" file). Your application must declare all its components in this file, which must be at the root of the application project directory.
    在Android系统启动一个程序组件之前,系统必须通过读取AndroidManifest文件来知道需要使用哪些组件,你的程序必须声明所有的组件在那个文件里面,文件在工程的根目录下。

    The manifest does a number of things in addition to declaring the application's components, such as:

    • Identify any user permissions the application requires, such as Internet access or read-access to the user's contacts.
      确认程序访问网络,读取用户通讯录的权限等
    • Declare the minimumAPI Levelrequired by the application, based on which APIs the application uses.
      声明程序的最低的APILevel
    • Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen.
      声明程序需要使用到硬件与软件的功能
      API libraries the application needs to be linked against (other than the Android framework APIs), such as theGoogle Maps library.
      除了Android Frame API文件里面的Library需要进行链接

    Declaring components


    如下声明Manifest文件

    ... In theelement, theandroid:iconattribute points to resources for an icon that identifies the application
    设置应用的图标

    In theelement, theandroid:nameattribute specifies the fully qualified class name of theActivitysubclass and theandroid:labelattributes specifies a string to use as the user-visible label for the activity.
    设置activity的显示名称

    You must declare all application components this way:

    • elements for activities
    • elements for services
    • elements for broadcast receivers
    • 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 (asBroadcastReceiverobjects) and registered with the system by callingregisterReceiver()
    没有声明的组件对系统不可见,也不可以运行,然而,broadcast receiver可以在代码中被动态的创建,并且被系统使用registerReceiver进行注册

    Declaring component capabilities

    As discussed above, inActivating Components, you can use anIntentto 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.

    我们可以使用Intent来启动activity,service,broadcast Receiver。你可以在Intent中明确指定目标组件。然而Intent的体现依赖于Intent动作的概念。在Intent动作下,你可以简单的描述你希望执行的动作类型,并且允许系统在设备上找到相应的组件并启动它。如果有多种组件可以执行Intent描述的这个动作,那么用户来选择使用哪一个

    The way the system identifies the components that can respond to an intent is by comparing the intent received to theintent filtersprovided in the manifest file of other applications on the device.

    系统是与从Manifest文件中提供的Intent filter进行对比来选择相应的组件

    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 anelement as a child of the component's declaration element.

    当你在应用的Manifest文件中声明一个组件时,你可以选择是否添加Intent filter,它用来声明组件相应其他应用Intent的能力。你可以使用 标签作为组件声明标签的自标签来声明一个Intent filter。

    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 Android Market do read them in order to provide filtering for users when they search for applications from their device.
    有许多不同的Android设备,并不是所有设备都提供同样的功能与能力。为了防止你的应用装上设备后却发现设备缺少应用需要的功能,必须清楚的在Manifest文件中为你的应用支持的设备类型与软件需要定义属性。大多数声明仅仅是个信息而系统并不读取他们,但是外部服务如Android market在他们从设备上查找应用时会去读取filter。

    However, you can also declare that your applicaiton uses the camera, but does notrequireit. 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
    你可以声明你的应用来使用camera,当并不是必须的。在那种情况下,你的应用在执行的时候会去验证是否这个设备具有camera,并且会在其中一个需要不能提供的时候关闭所有的功能。

    下面是我们开发应用时需要考虑的设备属性:
    Screen size and density:屏幕大小与分辨率

    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 theelement.


    通常情况下,你的应用可以适应所有的屏幕大学与分辨率,因为Android系统会自动调整布局与图片资源。然而你需要为特定的屏幕大小指定布局文件,并指定特定的图像给确定的分辨率,使用可以替代的布局资源,并且在Manifest文件中正确的指出支持哪些屏幕大小。

    Input configurations:输入配置
    使用标签来声明指定的输入方式,其实很少情况下需要那样做

    Device features:设备功能
    使用标签来来声明设备是否有camera, a light sensor, bluetooth, a certain version of OpenGL, or the fidelity of the touchscreen等功能

    Platform Version:平台版本
    you should declare the minimum API Level in which those APIs were introduced using theelement.

    It's important that you declare all such requirements for your application, because, when you distribute your application on Android Market, Market 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.

    声明那些需求是很重要的,当我们发布应用到Android market,只有那些符合你声明需求的设备才会被可以使用。

    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 your application for a variety of device configurations (such as different languages and screen sizes).

    一个Android应用不仅仅是又代码组成的,它还需要图片,音频或者其他一切可呈现的东西。比如XML文件。使用资源文件使得不用修改代码就可以更新程序

    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 namedlogo.png(saved in theres/drawable/directory), the SDK tools generate a resource ID namedR.drawable.logo, which you can use to reference the image and insert it in your user interface.

    对于任何一个资源文件,Android SDK会生成唯一的一个ID来标识资源,我们可以使用ID来直接引用文件资源

    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 languagequalifierthat you append to the resource directory's name (such asres/values-fr/for French string values) and the user's language setting, the Android system applies the appropriate language strings to your UI。

    最重要的是,你需要为了不同的设备提供不同可选择的资源配置,比如在XML文件中定义不同的UI字符串,这样可以实现国际化,便于选择不同国家的语言。

    Android supports many differentqualifiersfor 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.

    Android系统为可选择的资源提供了不同的限定符,比如当设备垂直或者水平放置的时候,我们需要提供不同的解决方案,我们可以提供两套不同UI来应对不同的方向,系统会根据设备的方向自动选择对应的布局文件。


    注:有很多是自己简化后的理解,翻译的不好,请多多指教
















你可能感兴趣的:(【Android Dev Guide - 02】 - Application Fundamentals 应用基础)