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.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.
Android应用是用Java语言写的,所有的Java代码和相关的资源文件被Android SDK打成一个后缀为apk的Android包。一个APK包里所有的代码都被看做一个程序,并且用来安装应用程序到移动设备上。
Once installed on a device, each Android application lives in its own security sandbox.
一旦安装到设备上,每一个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:
然而,这里有很多方法使得不同程序之间共享数据,并且可以使得程序可以访问系统的服务。
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 ofActivity
and 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都会确保他正在运行,需要时会创建一个实例。
An intent is created with anIntent
object, which defines a message to activate either a specific component or a specifictypeof component—an intent can be either explicit or implicit, respectively.
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)。 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 theContentResolver
object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
对于其他组件类型,内容提供者,不是被Intent所激活,而是被目标contentResolver所激活。
Intent
tostartActivity()
orstartActivityForResult()
(when you want the activity to return a result).Intent
tostartService()
. Or you can bind to the service by passing anIntent
tobindService()
.Intent
to methods likesendBroadcast()
,sendOrderedBroadcast()
, orsendStickyBroadcast()
.sendBroadcast()
,sendOrderedBroadcast()
, orsendStickyBroadcast()
. query()
on aContentResolver
你可以执行一个查询通过调用query()
on aContentResolver
Before the Android system can start an application component, the system must know that the component exists by reading the application'sAndroidManifest.xml
file (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:
element, theandroid:icon
attribute points to resources for an icon that identifies the application In the
element, theandroid:name
attribute specifies the fully qualified class name of theActivity
subclass and theandroid:label
attributes 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 providersBroadcastReceiver
objects) and registered with the system by callingregisterReceiver()
没有声明的组件对系统不可见,也不可以运行,然而,broadcast receiver可以在代码中被动态的创建,并且被系统使用registerReceiver进行注册 As discussed above, inActivating Components, you can use anIntent
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 theintent filtersprovided 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
element as a child of the component's declaration element.
标签作为组件声明标签的自标签来
声明一个Intent filter。
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
element.
标签来声明指定的输入方式,其实很少情况下需要那样做
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 the
element.
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.
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.