Android Hierarchy Viewer
The Android Hierarchy Viewer see Figure 2.8 , a visual tool that illustrates layout component relationships, helps developers design and debug user interfaces. Developers can use this tool to inspect the View properties and develop pixel-perfect layouts. For more information about user interface design and the Hierarchy Viewer, see Chapter 8,Designing User Interfaces with Layouts. Figure 2.8 Screenshot of the Android Hierarchy Viewer in action. Figure 2.8 Screenshot of the Android Hierarchy...
Specifying the Target SDK Version
You should always specify the targetSdkVersion attribute for your application.This value represents the Android SDK version your application was built for and tested against. For example, if your application was built using the APIs that are backward-compatible to Android 1.6 API Level 4 , but targeted and tested using Android 2.2 SDK API Level 8 , then you would want to specify the targetSdkVersion attribute as 8.Therefore, add the following to your Android manifest file within the lt manifest...
Summary Wti
Each Android application has a specially formatted XML file called AndroidManifest.xml.This file describes the application's identity in great detail. Some information you must define within the Android manifest file includes the application's name and version information, what application components it contains, which device configurations it requires, and what permissions it needs to run. The Android manifest file is used by the Android operating system to install, upgrade, and run the...
Wireless Application Protocol WAP
As it turned out, allowing direct phone access to the Internet didn't scale well for mobile. By this time, professional websites were full color and chock full of text, images, and other sorts of media.These sites relied on JavaScript, Flash, and other technologies to enhance the user experience, and they were often designed with a target resolution of 800x600 pixels and higher. When the first clamshell phone, the Motorola StarTAC, was released in 1996, it merely had an LCD 10-digit segmented...
Using Activity Callbacks to Manage Application State and Resources
Different important state changes within the Activity lifecycle are punctuated by a series of important method callbacks.These callbacks are shown in Figure 4.3. Here are the method stubs for the most important callbacks of the Activity class public class MyActivity extends Activity protected void onCreate Bundle savedlnstanceState protected void onStart protected void onRestart protected void onResume protected void onPause protected void onStop protected void onDestroy protected void onResume...
Exploring Other Manifest File Settings
We have now covered the basics of the Android manifest file, but there are many other settings configurable within the Android manifest file using different tag blocks, not to mention attributes within each tag we already discussed. Some other features you can configure within the Android manifest file include Setting application-wide themes as lt application gt tag attributes Configuring instrumentation using the lt instrumentation gt tag Aliasing activities using the lt activity-alias gt tag...
Androids Underlying Architecture
The Android platform is designed to be more fault-tolerant than many of its predecessors. The handset runs a Linux operating system upon which Android applications are executed in a secure fashion. Each Android application runs in its own virtual machine see Figure 1.8 . Android applications are managed code therefore, they are much less likely to cause the phone to crash, leading to fewer instances of device corruption also called bricking the phone, or rendering it useless . The Linux 2.6...
Specifying Required Device Features
Not all Android devices support every Android feature. Put another way There are a number of APIs and related hardware that Android devices may optionally include. For example, not all Android devices have multi-touch ability or a camera flash. The lt uses-feature gt tag can be used to specify which Android features the application requires to run properly. These settings are for informational purposes only the Android operating system does not enforce these settings, but publication channels...
Setting Simple Resource Values Using Eclipse
Developers can define resource types by editing resource XML files manually and using the aapt to compile them and generate the R.java file or by using Eclipse with the Android plug-in, which includes some very handy resource editors. To illustrate how to set resources using the Eclipse plug-in, let's look at an example. Create a new Android project and navigate to the res values strings.xml file in Eclipse and double-click the file to edit it.Your strings.xml resource file opens in the right...
Dalvik Debug Monitor Server DDMS
The Dalvik Debug Monitor Server DDMS is a command-line tool that has also been integrated into Eclipse as a perspective see Figure 2.7 .This tool provides you with direct access to the device whether it's the emulator virtual device or the physical device.You use DDMS to view and manage processes and threads running on the device, view heap data, attach to processes to debug, and a variety of other tasks. Figure 2.7 Using DDMS integrated into an Eclipse perspective. Figure 2.7 Using DDMS...
Finding the Last Known Location
To add location support to MyFirstAndroidApp, edit the file MyFirstAndroidApp.java. First, you must add the appropriate import statements import android.location.Location import android.location.LocationManager Now, create a new method called getLocation in your class and make a call to this method in your onCreate method.The getLocation method gets the last known location on the phone and logs it as an informational message. If the operation fails for some reason, the method logs an error. The...
Stopping Saving and Releasing Activity Data in onPause
When another Activity moves to the top ofthe activity stack, the current Activity is informed that it is being pushed down the activity stack by way of the onPause method. Here, the Activity should stop any audio, video, and animations it started in the onResume method.This is also where you must deactivate resources such as database Cursor objects if you have opted to manage them manually, as opposed to having them managed automatically. Android provides a number of helper utilities for...
Tip Ded
Many of the code examples provided in this chapter are taken from the MyFirstAndroidApp application. This source code for the MyFirstAndroidApp application is provided for download on the book's website. Figure 3.6 The Android emulator launching locked . Figure 3.6 The Android emulator launching locked .
Tip Dut
You might want to use the onSaveinstanceState method to store nonessential information such as uncommitted form field data or any other state information that might make the user's experience with your application less cumbersome. When this Activity is returned to later, this Bundle is passed into the onCreate method, allowing the Activity to return to the exact state it was in when the Activity paused.You can also read Bundle information after the onStart callback method using the...
Creating an Android Virtual Device AVD for Your Snake Project
The next step is to create an AVD that describes what type of device you want to emulate when running the Snake application.This AVD profile describes what type of device you want the emulator to simulate, including which Android platform to support.You can specify different screen sizes and orientations, and you can specify whether the emulator has an SD card and, if it does, what capacity the card is. I -I . I I 1 j Cwa Figure 3.2 The Snake project details. For the purposes of this example,...
Adding Some Media Support to Your Application
Next, let's add some pizzazz to MyFirstAndroidApp by having the application play an MP3 music file.Android media player features are found in the MediaPlayer class of the android.media package. You can create MediaPlayer objects from existing application resources or by specifying a target file using a Uniform Resource Identifier URI . For simplicity, we begin by accessing an MP3 using the Uri class from the android.net package. Some methods in the android.media.MediaPlayer and android.net.Uri...
Debugging Your Android Application in the Emulator
Before we go any further, you need to become familiar with debugging in the emulator. To illustrate some useful debugging tools, let's manufacture an error in the My First Android Application. In your project, edit the file MyFirstAndroidApp.java and create a new method called forceError in your class and make a call to this method in your onCreate method. The forceError method forces a new unhandled error in your application. The forceError method should look something like this public void...








