Android apps are written in the java programming language.The Android SDK tools compile your code-along with any data and resource file-into an APK:an Android package,which is an archive file with an .apk suffix.One APK file contains all the contents of an Android app and is the file that Android-powered devices use to install the app.java
安卓程序是Java程序语言编写的。安卓SDK工具将编译你的代码、任何数据、资源文件一块儿打包成APK:以.apk后缀名的存档文件的一种Android包。一个APK文件包含全部Android程序的内容,全部使用Android的设备也是用APK文件来安装Android应用的。android
Once installed on a device,each Android app lives its own security sandbox:web
一旦安装在一个设备上,任何一个Android 应用都会运行在一个它本身的安全砂箱中。数据库
In this way,the Android system implements the "principle of least privilege".That is ,each app,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 app cannot access parts of the system for which it is not given permission.
这样,Android系统执行一种“最少特权原则”。那就是,每个应用程序,通常来讲,只有访问工做的组件的权限没有更多。这形成了一个很是安全的环境,每个应用程序不能得到到系统没有赋予权限的部分。安全
Howerver there are ways for an app to share data with other apps and for an app to access system service :网络
然而,应用程序也有许多方式和其余应用程序分享数据还有访问系统服务。并发
That convers the basics regarding how an Android app exists within the system.The rest of this document introduces you to:app
这些覆盖了一些基础问题,一个Android应用如何在系统中存在。剩下的文档将向你介绍:框架
App Components异步
App components are the essential building blocks of an Android app.Each component is a different point through which the system can enter your app.Not all components are actual entry points for the user and some depend on each other,but each one exist as its own entity and plays a specific role-each one is unique building block that helps define your app's overall behavior.
There are four different types of app components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
Here are four types of app components:
App组件是Android应用程序的基本的构造模块。每个组件都是不一样的点,系统能进入你的应用程序。不是全部的组件是真正的用户进入点,有一些也互相依赖,可是每个都做为本身的实体存在并且发挥一个特殊的做用——每个都是独一无二的建造块帮助定义你的应用程序的整体行为。
有四个不一样类型的应用程序组件。每个类型为一个明显的目的服务,而且有一个明显的生命周期,定义了组件怎么被建立和销毁。
这里有四种类型的应用程序组件:
Activities
An activity represents a single screen with a user interface.For example,an email app 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 app,each one is independent of the others.As such,a different app can start any one of these activities(if the email app allows it). For example,a camera app can start the email app 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.
一个activity表明用户界面的单个屏幕。例如,一个电子邮件应用程序也许有一个activity显示一个新邮件列表,另外一个activity构成一封电子邮件,另外一个activity来阅读电子邮件。尽管这些activity在电子邮件应用程序中一块儿工做构成一个紧密结合的用户经验,每个activity对于其余的来讲都是独立的。一样的,一个不一样的应用程序能启动他们中任何一个activity(只要Email app容许)。例如,一个照相app能启动Email app中的新建邮件的activity,为了用户分享一张图片。
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 app,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 an you can learn about it in the Services developer guide.
一个service是一个组件运行在后台为了执行耗时操做或者是为远程进程工做。一个service不提供用户界面。例如一个service能够在后台播放音乐当用户在另外一个不一样的app中,或者是在一个activity中不阻塞用户界面获取网络数据。一个组件,好比activity能够启动service让他运行或者是绑定service为了和它互相起做用。
Content providers
A content provider manages a shared set of app data.You can store the data in the file system,an SQLite database,on the web,or any other persistent storage location your app can access.Through the content provider,other apps 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 app 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 app and not shared.For example,the Note Pad sample app uses a content provider to save notes.
A content provider is implemented as a subclass of ContentProvider and must implement a stadard set of APIs that enable other apps to perform(执行) transactions.For more information,see the Content Providers developer guide.
一个content provider 管理一个共享的app数据集合。你能够存储数据在文件系统、一个SQLite数据库、网络中,或是其余任何你的应用能够访问的持久化存储地址。经过content provider,其余应用能够查询或者甚至修改这些数据(若是content provider容许)。例如Android系统提供一个content provider 管理用户的联系人信息。一样的,任何一个有适当权限的app能够查询联系人的content provider(例如ContactsContract.Data)来读或是写一个详细的联系人的信息。
content providers 在读写你应用中未被共享的私有数据也是有用处的。例如Note Pad应用中用一个content provider 来保存笔记。
一个content provider 是一个实现ContentProdider的子类,必须实现一套API标准确保其余应用执行事务。查看更多信息,请看Content Providers开发指南。
Broadcast reveivers
A broadcast receiver is a component that responds to system-wide broadcast announcement.Many broadcasts originate from the system-for example,a bropadcast announcing that the screen has turned off,the battery is low,or a picture was captured.Apps can also initiate broadcasts-for example,to let other apps 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 commponents 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.
A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.For more information,see BroadcastReceiver class.
一个broadcast receiver是一个响应系统范围的广播通知组件。许多广播是系统引发的,例如屏幕旋转的广播通知,电量太低或是一张图片被捕获。应用程序也能初始化广播,例如让其余应用知道一些数据被下载到设备并且对他们来讲是可用的。尽管broadcast receivers不提供用户界面,但当有一个广播事件发生的时候他们能建立一个状态栏通知来提示用用户。更常见的是broadcast receiver对于其余组件来讲仅仅是一个“通道”,他只作很是少的工做。例如他也许实例化了一个服务来执行一些基于这个事件的工做。
broadcast receiver 是一个实现了BroadcastReceiver的子类,并且每个broadcast都被做为一个Intent对象来交付。想要更多关于broadcast receiver的信息,请看BroadcastReceiver类。
A unique aspect of the Android system design is that any app can start another app's component.For example,if you want the user to capture a photo with the device camera,there's probably another app that does that and your app can use it,instead of developing an activity to capture a photo yourself.You don't nedd to incorporate or even link to the code from the camera app.Instead,you can simply start the activity in the camera app that captures a photo.When complete,the photo is even returned your app so you can use it.To the user,it seems as if the camera is actually a part of your app.
Android系统的一个独特的方面是任何应用程序均可以启动其余应用的组件。例如,若是你想要用户用照相机拍一张照片,颇有可能其余应用程序作了这个功能而你的应用能够调用它而不是开发一个Activity用来本身照相。你不须要合并或是从相机app中连接代码。替代的是,你能简单的启动相机app中的activity来拍摄照片。当照完照片的时候,照片会返回到你的应用而后你就可使用了。对于用户来讲,就好像照相机就是你应用程序的一部分同样。
When the system starts a component,it starts the process for that app(if it's not already running)and instantiates the classes needed for the component.For example,if your app starts the activity in the camera app that captures a photo,that activity runs in the process that belongs to the camera app,not in your app's process.Therefore,unlike apps on most other systems,Android apps don't have a single entry point(there's no main() function,for example).
当系统启动一个组件的时候,系统将会为所在的app启动进程(若是没有正在运行)还会实例化组件所须要的类。例如,若是你的app启动相机app的照相activity,拍照activity运行在相机app所在的进程中,而不是你的app所在的进程。所以,不像大多数其余操做系统中的应用程序,Android 应用程序没有一个单独的程序入口(没有main()方法,例如)。
Because the system runs each app in a separete process with file permissions that restrict access to other apps,your app cannot directly activate a component from other app.The Android system,however,can.So,to activate a component in another app,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.
由于系统运行应用程序在文件权限下的分开的进程中,进入其余应用有限制条件,因此你的应用程序不能直接激活其余app的组件。可是Android系统能够。因此为了激活其余应用中的组件,你必须向系统发送一条消息而且指定你的intent来启动一个特别的组件。而后系统将会为你激活相应的组件。
Activity Components
Three of the four component types-activities,services,and broadcast receivers-are activated by an asynchronous message called an intent.Intents bind individual componnets to each other at runtime(you can think of them as the messengers that request an action from ohter components),whether the component belongs to your app or anohter.
四大组件中有三个-activity、service、broadcast receiver,都是被一个叫作intent的异步消息激活的。intent在运行时绑定到独特的组件(你能够吧他们当作从其余组件请求动做的送信者),无论这个组件是否是属于你的应用程序。
An intent is created with an Intent object,which defines a message to activate either specific(特殊的) component or a specific type of 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 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).
对于activity和service来讲,一个intent定义要执行的动做(例如“查看”或“发送”东西)还有可能指定将要对其起做用的数据的URI(除了别的以外将要启动的组件须要知道)。例如一个intent可能为一个Activity传递一个请求来显示一张图片或是打开一个网页。在某种状况下,你能够开启一个Activity来接收一个结果,在这种状况下,这个Activity也在intent中返回结果(例如你能够发布一个intent来让用户选择一个联系人而后把它返回给你,这个返回的intent包括一个指向选择的联系人的URI指针)。
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").
对于broadcast receiver来讲,intent简单的定义了被广播的通知(例如一个指明设备电量太低的广播仅仅包括一个动做字符串指明“电量太低”)。
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).
其余的组件类型,content provider不是被intent激活的。当被一个叫作ContentResolver的请求做为目标的时候才被激活。内容解析器与content provider直接处理事务,这样组件与conent provider就不须要执行交易了,而是调用ContentResolver对象上的方法。在content provider和须要查询信息的组件之间就离开了一层抽象(在安全性上来讲)。
There are separate methods for activating each type of component:
激活每一种类型的组件有不一样的方法:
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.
查看更多关于使用intent,请查看Intents and Inent Filters文档。更多信息关于激活特殊组件也被提供在如下文档:Activities,Services,BroadcastReceiver和Content Providers.
The Manifest File
Before the Android system can start an app component,the system must know that the component exists by reading the app's AndroidManifest.xml file(the "mainfest"file).Your app must declare all its componet in this file,which must be at the root of the app project drectory.
在Android系统在启动一个应用程序组件以前,系统必须得经过读取应用的AndroidManifest.xml文件("清单"文件)来知道存在的组件。你的应用程序必须在这个文件中定义全部的用到的组件,清单文件也必须在应用程序项目的根目录下。
The manifest does a number of things in addition to declaring the app's components, such as:
清单文件除了定义应用程序组件以外还有有一些东西,例如:
Declaring components
The primary task of the manifest is to inform the system about the app's components. For example, a manifest file can declare an activity as follows:
清单的主要任务是告诉系统有哪些应用程序组件。例如,一个清单文件能够定义一个activity以下:
<?xml version="1.0" encoding="utf-8"?> <manifest ... > <application android:icon="@drawable/app_icon.png" ... > <activity android:name="com.example.project.ExampleActivity" android:label="@string/example_label" ... > </activity> ... </application> </manifest>
In the <application> element, the android:icon attribute points to resources for an icon that identifies the app.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.
在<application>元素中,"android:icon"属性指定了资源中的一个图标来识别应用程序。在<activity>元素中,"android:name"属性指明了整个限制性的Activity子类的类名还有"andorid:label"元素指明了一个为activity用做用户可见的标签字符串。
You must declare all app components this way:
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().
你在源文件中包含的Activitiy、service和conent provider而没有在清单文件中定义在系统中是不可见的,所以,不能运行。然而,broadcast receiver既能在清单文件中定义或是动态的在代码中被建立(做为BroadcastReciver对象)经过调用registerReceiver()方法来注册。
For more about how to structure the manifest file for your app, see The AndroidManifest.xml File documentation.
Declaring component capabilities
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 implicit intents. An implicit intent simply describes the type of action to perform (and, optionally 随意地, the data upon which you’d like to perform the action) and allows 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来启动activites,services,还有broadcast receivers.你能够在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 the intent filters provided in the manifest file of other apps on the device.
系统能够识别对某intent做出响应的组件方式是经过将接受到的intent和设备中其余程序的manifest文件的intent filters进行比较来实现的。
When you declare an activity in your app's manifest, you can optionally include intent filters that declare the capabilities of the activity so it can respond to intents from other apps. You can declare an intent filter for your component by adding an <intent-filter> element as a child of the component's declaration element.
当你在你的应用程序清单中定义一个activity时,你能够随意的包含intent filters,这些intent filters定义了activity的能力它能响应其余应用中的intent.你能够为你的组件定义一个intent filter经过添加一个<intent-filter>元素做为组件声明元素的子元素。
For example, if you've built an email app with an activity for composing a new email, you can declare an intent filter to respond to "send" intents (in order to send a new email) like this:
例如,若是你已经构造了一个电子邮件应用程序,它有一个写Email的Activity,你能够定义一个intent filter来回应"发送"intents(为了发送一个新邮件)像这样:
<manifest ... > ... <application ... > <activity android:name="com.example.project.ComposeEmailActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <data android:type="*/*" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
Then, if another app creates an intent with the ACTION_SEND action and pass it to startActivity(), the system may start your activity so the user can draft and send an email.
而后,若是其余应用程序建立一个包含ACTION_SEND action的intent而后把它传给startActivity()方法,系统启动你的activity而后用户能够起草并发送一个email.
For more about creating intent filters, see the Intents and Intent Filters document.
Declaring app 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 app from being installed on devices that lack features needed by your app, it's important that you clearly define a profile for the types of devices your app 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 apps from their device.
有各类各样的设备是Android设备,但全部的设备没有提供相同的特性和性能。为了防止你的应用在缺乏你应用须要的特性上的设备上安装,你清楚的定义一个你应用支持设备的简况经过在manifest文件中声明所须要的设备和软件是很重要的。大部分的声明仅仅是声明的,系统并不去读它,可是外部服务例如Google Play会读取它,为了当用户为他们的设备搜索应用程序的时候提供过滤器。
For example, if your app requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file like this:
例如,若是你的app须要一个照相机而且须要Android2.1(API Level 7)的APIs,你应当在你的清单文件中像这样声明:
<manifest ... > <uses-feature android:name="android.hardware.camera.any" android:required="true" /> <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" /> ... </manifest>
Now, devices that do not have a camera and have an Android version lower than 2.1 cannot install your app from Google Play.
However, you can also declare that your app uses the camera, but does not require it. In that case, your app must set the required attribute to "false" and check at runtime whether the device has a camera and disable any camera features as appropriate.
如今呢,没有照相机或是Android版本低于2.1的设备将不能从Google Play中安装你的设备。
而后,你也能定义你的应用程序使用照相机,可是不须要照相机。在这样的状况下,你的应用程序必须设置须要的属性为"false"而后检查运行时是否设备有照相机而后视状况而定的将相机属性设为不可用。
More information about how you can manage your app's compatibility with different devices is provided in the Device Compatibility document.
App Resources
An Android app 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 app. For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files. Using app resources makes it easy to update various characteristics of your app without modifying code and—by providing sets of alternative resources—enables you to optimize your app for a variety of device configurations (such as different languages and screen sizes).
一个Android 应用程序不只仅是代码组成的,他也须要与代码分隔开来的资源,例如图像,媒体文件,还有任何一些App相关的可视图像。例如,你可使用XML文件来定义动画,菜单,样式,颜色还有用户接口的activity。用app资源使更新你应用的的特征变得很简单,不用修改源代码(经过提供供选择的资源集合)可以使你为不一样的设备配置优化你的App(例如不一样的语言和屏幕尺寸)。
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 app code or from other resources defined in XML. For example, if your app 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.
为每个你在Android项目中包含的资源,SDK建立工具定义了独一无二的ID,你能够在你的代码中引用资源或是XML其余定义的资源。例如,若是你的App包含了一个图像文件叫作logo.png(保存在res/drawable/文件夹中),SDK工具生成一个资源ID叫作R.drawable.log,你能够用使用它来引用图像还能够在你的用户界面中插入这张图像。
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.
提供将资源从你的源代码中分隔开来最重要的方面之一是为你提供了为不一样设备配置提供了可选择的资源。例如经过在XML定义UI字符串,你能够将字符串翻译为其余语言而后保存那些字符串在分开的文件中。而后基于一种语言的限定你能够扩展资源文件夹的名字(例如res/values-fr/为法语字符串资源)和用户语言设置,Android系统将为你的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.
Android为你的可选资源支持许多不一样的限定。修饰是你的资源文件夹的一个简短的字符串,为了给那些须要用到的资源定义设备配置。再如,你能够根据设备屏幕方向和大小为你的Activity建立不一样的layouts。好比,当设备屏幕是纵向方向的(高的),你能够想要一个拥有按钮纵向排列的布局,但当屏幕是横向排列(宽的),按钮应该按照横向排列。根据方向更名布局,你能够定义两个不一样的布局,而后申请适当的限定给不一样的布局文件夹名字。(qualifier原来是资源文件中的文件夹名)。而后系统根据当前设备方向申请适当的布局。
For more about the different kinds of resources you can include in your application and how to create alternative resources for different device configurations, read Providing Resources.