Book Review: Learning Android by Marko Gargenta (O'Reilly Book)
Jump to navigation Jump to search
Learning Android (http://ofps.oreilly.com/titles/9781449390501/index.html) by Marko Gargenta O'Reilly Books
- 1 Chapter 1: Android Overview
- 2 Chapter 2: The Stack
- 3 Chapter 3: Quick Start
- 4 Chapter 4: Main Building Blocks
- 5 Chapter 5: Yamba Project Overview
- 6 Chapter 6: Android User Interface
- 7 Chapter 7: Preferences, the Filesystem, the Options Menu, and Intents
- 8 Chapter 8: Services
- 9 Chapter 9: The Database
- 10 Chapter 10: Lists and Adapters
- 11 Chapter 11: Broadcast Receivers
- 12 Chapter 12: Content Providers
- 13 Chapter 13: System Services
- 14 Chapter 14: The Android Interface Definition Language
- 15 Chapter 15: The Native Development Kit (NDK)
Chapter 1: Android Overview
- A snapshot of the Android device distribution http://developer.android.com/resources/dashboard/platform-versions.html
Chapter 2: The Stack
- Android stacks
- Linux kernel (display, camera, keypad, etc.)
- Native libraries (Android runtime (CoreLibs, Delvik VM), OpenGL, SSL, SQLite, Webkit, etc.)
- Application framework (Activity Manager, Content providers, Notification manager, etc.
- Applications (Home, Contacts, Phone, Browser, etc.)
- GNU libc, the default C library for Linux which Android is using, is licensed under GPL license, which requires any changes we release to be pushed back to the open source community. On the other hand, Android and some third-party open source libraries are licensed under an Apache/MIT license, which does not require derivative works to be open-sourced.
- Dalvik is a purpose-built virtual machine designed specifically for Android, developed by Dan Bornstein and his team at Google.
- While Java language, Java tools and Java libraries are free, the Java VM is not. Dalvik VM in Android is to replace the Java VM.
- Differences between Android and Java
- Java source code => (Java compiler) => Java byte code (running on Java VM).
- Java source code => (Java compiler) => Java byte code => (Dex compiler) => Dalvik byte code => Dalvik executable (running on Dalvik VM).
- Android Java is a nonstandard collection of Java classses. Its set of libraries is closet to Java Standard Edition.
- The APK is a single application package file. It has three main components:
- Dalvik executable = compiled code to executable
- Resources = non-code - images, audio, etc.
- Native libraries (optional) = some native code if any.
Chapter 3: Quick Start
- Android SDK download page - http://developer.android.com/sdk
- Installing the SDK http://developer.android.com/sdk/installing.html
- Installing Eclipse http://eclipse.org/ - Eclipse IDE for Java Developers (not the twice-as-large Eclipse for Java EE Developers) is recommended.
- Project files:
- Manifest file has all application's main building blocks, what permission is required to run.
- Layout XML code files specify the layout of the screen.
- Strings (another XML file) contains all texts that application uses.
- The R file is the glue between the Java and resources. It's automatically generated file.
- Java source code contains all codes which will be converted to a Dalvik executable.
- Android SDK ships with a true emulator based on QEMU.
Chapter 4: Main Building Blocks
- An Activity is usually a single screen on the device.
- Activity Life Cycle
- Launching an activity is expensive.
- Transition from the Starting state to Running state is the most expensive.
- The Running State is when one running activity is in focus.
- The Paused state is when an activity is not in focus - showing dialog boxes
- The Stopped state is when an activity is not visible, but still in memory (just in case if user might get back to it, restarting the Stopped activity is far cheaper then starting from scratch.
- The Destroyed state is when the destroyed activity is no longer in memory.
- The Intents are messages that are communicated among building blocks.
- Services run in the background, and can perform the same actions as the activities, but they don't have any user interfaces.
- Content Providers
- They are interfaces for sharing data between applications.
- The Content Provider API adheres to the CRUD principle http://en.wikipedia.org/wiki/Create,_read,_update_and_delete.
- For example, the Contacts Provider is a content provider that exposes all user contact data to other applications. The Contacts app itself does not have any contacts data, and Contacts Provider does not have any user interface.
- Broadcast Receivers
- Broadcast Receivers are using the publish/subscribe mechanism (or Observer pattern).
- System sends broadcast events all the time, e.g., when an SMS arrives, or when it reboots, etc.
- Broadcast Receivers do not have any user interfaces, nor are they actively running in memory. When triggered, they will execute some codes such as starting an activity, or a service.
- Application Context
- Application Context refers to the application environment and the processes within which all its components (activity, service, content provider, and broadcast receiver) are running. It allows applications to shared data and resources among components.
- An application Context is created whenever the first component of this application is started up.
Chapter 5: Yamba Project Overview
- Design Philosophy
- Small increments
- Always whole and complete
- Refactoring code
Chapter 6: Android User Interface
- Two ways to create a user interface
- Declarative User Interface using XML - WYSIWYG
- Programmatic User Interface using Java code
- Best practice is to use both methods.
- Views and Layouts
- Views - button, label, text box, and so on. This sometime is called Widget (not App Widget).
- Layouts organize views (grouping, location)
- LinearLayout - simplest, layout_orientation (vertical or horizontal)
- RelativeLayout can minimize the total number of views, thus improving performance
- Important Widget properties
- layout_height and layout_width
- fill_parent - widget wants all available space fro its parent
- wrap_content - widget requires only as much space as it needs to display its own content
- In the API level 8 or higher, the fill_parent has been renamed to match_parent.
- layout_weight - a number between 0 and 1
- layout_gravity - top, center, left, and so on
- gravity - the content of the widget withing the widget itself
- text - best practice is to use the pre-defined text in the string.xml
- id - unique identifier
- layout_height and layout_width
Chapter 7: Preferences, the Filesystem, the Options Menu, and Intents
- Create a Preference resource files called prefs.xml.
- Implement the PrefsActivity.java file that inflates that resource file.
- Register this new activity with the AndroidManifest.xml file.
- Provide a way to start that activity from the rest of the application.
- The Options Menu
- Create the menu.xml resource where we specify what the menu consists of.
- Add onCreateOptionsMenu() to the activity that should have this menu.
- Provide handling of menu events in onOptionsItemSelected().
Chapter 8: Services
- Service is a piece of codes running in background with no user interface.
- A service can be bound or unbound.
- Bound services can provide more specific APIs to other applications via an interface called AIDL (Android Interface Definition Language).
- Unbound services have its own life cycle (independent from that of the activities that start them). They have only two states - started and stopped (destroyed).
Chapter 9: The Database
- SQLite is an open source database.
- It’s a a single-file and zero-configuration database.
- Android framework, called SQLiteOpenHelper, provides an easy and elegant way to interact with an SQLite database.
- Currently the framework supports only for CRUD operations: Create (INSERT), Read (SELECT), UPDATE, and DELETE.
- It's better to use the DbHelper than using SQL directly.
- Using SQL statement may pose the potential security (from SQL injection) and performance (from SQL parsing).
- It's a good practice to handle all the SQLExceptions by surrounding your database calls in try/catch blocks
Chapter 10: Lists and Adapters
- A ScrollView can contain only one direct child. If there is a need to combine multiple views into a single view that scrolls, those views must be organized into another layout first.
- Adapters generally have two flavors: those that represent array data and those for cursor data.
- The cursor-based adapter is well-suited for database.
- One of the simplest of those Adapters is SimpleCursorAdapter.
Chapter 11: Broadcast Receivers
- Broadcast receivers are implemented using the Publish/Subscribe messaging pattern, or the Observer pattern.
- Applications (known as publishers) can generate broadcasts to send events (without a need to worry about who the recipients are).
- Receivers (known as subscribers) that want the information will subscribe to specific messages via filters. If the message matches a filter, the subscriber is activated (if it’s not already running) and notified of the message.
Chapter 12: Content Providers
- Content providers let you centralize content in one place and have many different applications access it as needed.
- To create a content provider:
- Create a new Java class that subclasses the system’s ContentProvider class.
- Declare your CONTENT_URI.
- Implement all the unimplemented methods, such as insert(), update(), delete(), query(), getID(), and getType().
- Declare your content provider in the AndroidManifest.xml file.
- The Uniform Resource Identifier (URI) is used to locate a content provider.
- Part A, content://, is always set to this value.
- Part B, com.company.provider, is the so-called authority. It is typically the name of the class, all in lowercase. This authority must match the authority that we specify for this provider when we later declare it in the manifest file.
- Part C, status, indicates the type of data that this particular provider provides. It could contain any number of segments separated with a slash, including none at all.
- Part D, 47, is an optional ID for the specific item that we are referencing. If not set, the URI will represent the entire set. Number 47 is an arbitrary number picked for this example.