【正文】
Android Key ConceptsNow that you have an idea of what Android is, let’s take a look at how it works. Some parts of Android may be familiar, such as the Linux kernel,OpenGL, and the SQL database. Others will be pletely foreign,such as Android’s idea of the application life ’ll need a good understanding of these key concepts in order to writewellbehaved Android applications, so if you read only one chapter in this book, read this one. The Big Picture Let’s start by taking a look at the overall system architecture—the key layers and ponents that make up the Android open source software stack. In Figure , on the next page, you can see the “20,000foot”view of Android. Study it closely—there will be a test layer uses the services provided by the layers below it. Starting from the bottom, the following sections highlight the layers provided by Android.Linux KernelAndroid is built on top of a solid and proven foundation: the Linux kernel. Created by Linus Torvalds in 1991, Linux can be found today in everything from wristwatches to superputers. Linux provides the hardware abstraction layer for Android, allowing Android to be ported to a wide variety of platforms in the future. Internally, Android uses Linux for its memory management, process management, networking, and other operating system services. The Android phone user will never see Linux, and your programs will not make Linux calls directly. As a developer, though, you’ll need to be aware it’s there.Some utilities you need during development interact with Linux. For example, the adb shell mand1 will open a Linux shell in which you can enter other mands to run on the device. From there you can examine the Linux file system, view active processes, and so forth, subject to security restrictions.Native LibrariesThe next layer above the kernel contains the Android native shared libraries are all written in C or C++, piled for the particular hardware architecture used by the phone, and preinstalled by the phone vendor.Some of the most important native libraries include the following:? Surface Manager: Android uses a positing window manager similar to Vista or Compiz, but it’s much simpler. Instead of drawing directly to the screen buffer, your drawing mands go into offscreen bitmaps that are then bined with other bitmaps to form the display the user sees. This lets the system create all sorts of interesting effects such as seethrough windows and fancy transitions.? 2D and 3D graphics: Two and threedimensional elements can be bined in a single user interface with Android. The library will use 3D hardware if the device has it or a fast software renderer if it doesn’t. See Chapter 4, Exploring 2D Graphics, on page 73 and Chapter 10, 3D Graphics in OpenGL, on page 198.? Media codecs: Android can play video and record and play backaudio in a variety of formats including AAC, AVC (), , MP3, and MPEG4. See Chapter 5, Multimedia, on page 105 for an example.? SQL database: Android includes the lightweight SQLite database engine,2 the same database used in Firefox and the Apple You can use this for persistent storage in your application. See Chapter 9, Putting SQL to Work, on page 178 for an example.? Browser engine: For the fast display of HTML content, Android uses the WebKit This is the same engine used in the Google Chrome browser, Apple’s Safari browser, the Apple iPhone, and Nokia’s S60 platform. See Chapter 7, The Connected World, on page 130 for an example.These libraries are not applications that stand by themselves. They exist only to be called by higherlevel programs. Starting in Android , you can write and deploy your own native libraries using the Native Development Toolkit (NDK). Native development is beyond the scope of this book, but if you’re interested, you can read all about it Android RuntimeAlso sitting on top of the kernel is the Android runtime, including the Dalvik virtual machine and the core Java libraries.The Dalvik VM is Google’s implementation of Java, optimized for mobile devices. All the code you write for Android will be written in Java and run within the VM. Dalvik differs from traditional Java in two important ways:? The Dalvik VM runs .dex files, which are converted at pile time from standard .class and .jar files. .dex files are more pact and efficient than class files, an important consideration for the limited memory and batterypowered devices that Android targets.? The core Java libraries that e with Android are different from both the Java Standard Edition (Java SE) libraries and the Java Mobile Edition (Java ME) libraries. There is a substantial amount of overlap, however. In Appendix A, on page 278, you’ll find a parison of Android and standard Java libraries.Application FrameworkSitting above the native libraries and runtime, you’ll find the Application Framework layer. This layer provides the highlevel building blocks you will use to create your applications. The framework es preinstalled with Android, but you can also extend it with your own ponents as needed.The most important parts of the framework are as follows:? Activity Manager: This controls the life cycle of applications (see Section , It’s Alive!, on page 35) and maintains a mon“backstack” for user navigation.? Content providers: These objects encapsulate data that needs to be shared between applications, such as contacts. See Section , Content Providers, on page 40.? Resource manager: Resources are anything that goes with your program that is not code. See Section , Using Resources, on page 40.? Location manager: An Android phone always knows where it is. See Chapter 8, Locating and Sensing, on page 161.? Notification manager: Events such as arriving messages, appointments, proximity alerts, alien invasions