Now you will be creating your first Android Activity here.Here examines the application-building process from start to finish. I will show you how to create an Android project in Eclipse, add code to the initial files, and run the finished application in the Android Emulator. The resulting application will be a fully functioning program running in an Android environment.
Actually, as you move through this excercise, you will be creating more than one Android Activity. Computer programming tradition dictates that your first application be the typical Hello World! application, so in the first section you will create a standard Hello World! application with just a blank background and the “Hello World!” text. Then, for the sake of enabling you to get to know the language better, the next section explains in detail the files automatically created by Android for your Hello World! application. You will create two iterations of this Activity, each using different techniques for displaying information to the screen. You will also create two different versions of a Hello World! application that will display an image that delivers the “Hello World!” message. This will give you a good introduction to the controls and inner workings
You will often see “application” and “Activity” used interchangeably. The difference between the two
is that an application can be composed of multiple Activities, but one application must have at least one Activity. Each “window” or screen of your application is a separate Activity. Therefore, if you create a fairly simple application with only one screen of data (like the Hello World! application in previous Posts), that will be one Activity. In future Posts you will create applications with multiple Activities.
To make sure that you get a good overall look at programming in Android, create both of these applications in the Android SDK command-line environment for Microsoft Windows and Linux. In other words, this here covers the creation process in Eclipse, and we covers the creation process using the command-line tools. Therefore, before continuing, you should check that your Eclipse environment is correctly configured. Review the steps in post for setting the PAT statement for the Android SDK. You should also ensure that the
If you have configuration-related issues while attempting to work with any of the command-line examples, try referring to the configuration steps in before ; and look at the Android SDK documentation.
If you have a decent amount of experience as an application developer, you are familiar with the concept of an application life cycle. An application life cycle consists of the steps that the application’s processes must follow from execution to termination. Every application, regardless of the language it was written in, has a specific life cycle, and Android applications are no exception. This section examines the life cycle of an ASP application and compares that to an Android application’s life cycle.
Standard ASP Application Life Cycle The life cycle of a standard ASP application is similar enough to that of an Android application to make this a good comparison. ASP applications step through five distinct processes from launch to disposal. These steps are required to be implemented by all ASP applications, and really define what an ASP application is. The steps, in order, are
TIP Some ASP reference materials consider Disposal and Application_End to be one step in the life cycle. However, the Disposal call can be intercepted before it is passed to Application_End. This can allow the application to perform specific functions before it is actually destroyed. Application_Start is called when the application is requested from the server. This process in turn leads into the Event process(es). When all associated application modules have loaded, HTTPApplication.Init is called. The application executes its events, and when the user attempts to close it, Dispose is called. Dispose then passes processing on to the Application_End process, which closes the application.
This is a fairly standard application life cycle. Most applications follow similar life cycles: an application is created, loaded, has events, and is destroyed. The following section demonstrates how this compares to the Android application life cycle. Android Application Life Cycle The Android application life cycle is unique in that the system controls much of the life cycle of the application. All Android applications, or Activities, are run within their own process. All of the running processes are watched by Android and, depending on how the activity is running (this is, a foreground activity, background activity, and so forth), Android may choose to end the activity to reclaim needed resources.
NOTE When deciding whether an activity should be shut down, Android takes into account several factors, such as user input, memory usage, and processing time.
Some of the specific methods called during the life cycle of an android activity are
1 onCreate 2 onStart 3 Process-specific events (for example: launching activities or accessing a database) 4 onStop 5 onDestroy Following the same logic as other application life cycles, an Android application is created, the processes are started, events are fired, processes are stopped, and the application
ActivityCreator activityCreator (.bat or .pn) is a simple command-line tool that is used to set up a basic development environment. When run from the command line, activityCreator will set up the shell files needed to create a basic Android application. Having these shell files is especially useful if you are not using Eclipse. The Android plugin for Eclipse sets up these shell files for you by calling the activityCreator when you create a new project. Depending on what type of environment you are running, you will see the activityCreator represented by a different type of script file. If you are in a Windows environment, this will be a .bat file; otherwise it will be a python (.pn) script. You simply execute the script, which in turn calls the actual activityCreator process with the correct parameters.
APIs The API, or application programming interface, is the core of the Android SDK. An API is a collection of functions, methods, properties, classes, and libraries that is used by application developers to create programs that work on specific platforms. The Android API contains all the specific information that you need to create applications that can work on and interact with an Android-based application. The Android SDK also contains two supplementary sets of APIs—the Google APIs and the Optional APIs. Subsequent chapters will focus much more on these APIs as you begin writing applications that utilize them. For now, take a quick look at what they include so that you are familiar with their uses.
Google APIs The Google APIs are included in the Android SDK and contain the programming references that allow you to tie your applications into existing Google services. If you are writing an Android application and want to allow your user to access Google services through your application, you need to include the Google API. Located in the android.jar file, the Google API is contained within the com.google.* package. There are quite a few packages that are included with the Google API. Some of the packages that are shipped in the Google API include those for graphics, portability, contacts, and calendar utilities. However, the packages devoted to Google Maps will be the primary focus of us.
Using the com.google.android.maps package, which contains information for Google Maps, you can create applications that interact seamlessly with the already familiar interface of Google Maps. This one set of packages opens a whole world of useful applications just waiting to be created. The Google API also contains a useful set of packages that allows you to take advantage of the newer Extensible Messaging and Presence Protocol (XMPP) developed by the Jabber open source community. Using XMPP, applications can quickly become aware of other clients’ presence and availability for the purpose of messaging and communications. The API packages dealing with XMPP are very useful if you want to create a “chat”-style program that utilizes the phone messaging capabilities.
Optional APIs The Android SDK includes a number of Optional APIs
that cover functionality not covered by the standard Android APIs. These Optional APIs are considered optional because they deal with functionality that may or may not be present on a given handset device. That is, some devices created for the Android platform may include upgrades and features that others do not; the Optional APIs cover your programming options when trying to utilize these features in your Android applications. One of these optional features is a cell-phone-based GPS. The Android LBS (Location-Based Services) API deals with the functionality needed to receive and utilize information from a device’s GPS unit. (Combine the information in the Android LBS API with that in the Google Maps API, and you might have a very useful application that can automatically display a map of where you are located at any given point in time.) Other Optional APIs include those for utilizing Bluetooth, Wi-Fi, playing MP3s, and accessing 3-D—OpenGL-enable hardware.
The Android SDK supplies developers with a number of powerful and useful tools. Throughout this book, you will use only a handful of them directly. This section takes a quick look at just a few of these tools, which will be covered in much more depth in the following chapters, as you dive into command-line development.
For more detailed information about the other tools included in the Android SDK, consult the Android doc files.
Arguably one of the most important tools included in the Android SDK is emulator.exe. emulator.exe launches the Android Emulator. The Android Emulator is used to run your applications in a pseudo-Android environment. Given that, as of the writing of this book, there were no hardware devices yet released for the Android platform, emulator.exe is going to be your only means to test applications on a “native” platform.
You can run emulator.exe from the command line or execute it from within Eclipse. However, in the interest of giving you all the information you need to program with the Android SDK outside of Eclipse, Chapter 6 covers command-line usage of emulator.exe when you create your Hello World! applications.
When using the Android Emulator to test your applications, you have
two choices for navigating the user interface. First, the emulator comes with usable buttons, as shown in Figure 4-1. You can use these buttons to navigate Android and any applications that you develop for the platform.
The Power On/Off, Volume Up, and Volume Down buttons are
slightly hidden to the sides of the virtual device. They identify themselves when you hover the mouse pointer over them.Given that many higher-end phones now include a touch screen, the second input choice you have when using the emulator is a simulated touch screen. You use your mouse as a stylus. The objects on the emulator’s screen can be interacted with using the mouse.
Another tool that will become very useful to you when you are using command-line programming is Android Debug Bridge or adb (adb.exe). This tool allows you to issue commands to the Emulator.exe tool.
When you are working in a command-line environment, the adb tool allows you to do the following:
● Start and stop the server
● Install and uninstall applications
● Move files to and from the emulator
MKSDCARD.exe is a very useful tool if you are testing an application that will need to read or write files to or from an SD Memory Card inserted into the mobile
device. MKSDCARD.exe creates a small partition drive on your drive that will hold and retain the test files. The emulator will treat this partition like an SD Memory Card.
DX.exe is the compiler of the Android SDK. When run against your Java files, DX.exe will create files with .dex extensions—Dalvik executable format. These files are in the correct format to be understood by, and run on, an Android device.
Android executable files are called Dalvik executable files as a reference to the Dalvik virtual machine that Android used to run all applications. The Dalvik virtual machine runs each application in its own thread with the same priority as core Android applications.
Using Eclipse, load the API Demos application as a New Android Project. To do this, select File | New | Project from the Eclipse menu bar; a New Android Project wizard opens. Do not worry about the options in this wizard for now. Simply select Create Project From Existing Source and browse to the folder with the API Demo application in it. When the project is loaded, choose Run to see it execute in the Android Emulator. Navigate your way through the more than 40 different applications. Use each application to become familiar with the
terminology and function of each API tool it demonstrates.
The Hello, Activity! application, shown in the following illustration, is a simple Hello World!–style application. Though simple in its design, Hello, Activity! does a good job of showing off the abilities of the platform. You will create your own Hello World!–style applications soon.
Lunar Lander Lunar Lander, shown next, is a small game that plays on the Android Emulator. Lunar Lander shows how a simple 2-D game works on Android. The controls are fairly simple, and the game is not very complex. However, given these drawbacks, it is a great starter for game development.
Lunar Lander implements a simple control scheme (Up, Down, Left, and Right). The game also displays relatively fluid graphics and looks impressive given the platform. Complex game theories such as collision detection are used in a simple way. Although this book does not cover programming games for the Android platform, if you are interested in doing so, you may want to look at Lunar Lander for some tips.
Note Pad Note Pad, as shown in the illustration that follows, allows you to open, create, and edit small notes. Note Pad is not a full-featured word editor, so do not expect
it to be something to rival Word for Windows Mobile. However, it does a good job as a demonstration tool to show what is possible with a relatively small amount of code.
Skeleton App Skeleton App, shown next, is an application shell. This is more of a base application that demonstrates a couple of different application features, such as fonts, buttons, images, and forms. If you are going to run Skeleton App by itself, you really are not going to get much out of it. You will be better served by referring to Skeleton App as a resource for how to implement specific items.
Snake The final demo that is included with the Android SDK is Snake. This is a small, SNAFU-style game that is far more simplistic than Lunar Lander. This illustration shows what Snake looks like when run.
If you navigate to the base folder of each of the sample applications, you will see a folder named src. This is the source code folder for the given sample application. You can use this to view, edit, and recompile the code for any of the applications. Take advantage of this source code to learn some tricks and tips about the Android platform.
The Samples folder, ../%sdk folder%/SAMPLES, contains six sample applications that demonstrate a good cross-section of Android functionality:
● API Demos
● Hello, Activity!
● Lunar Lander
● Note Pad
● Skeleton App
These sample applications are provided by Google to give you a quick idea of how to develop an Android application. Each sample application demonstrates a different piece of Android’s functionality. You can open and run these applications from within Eclipse. Following is a brief description of each.
The API Demos application is a host application that demonstrates multiple API functions
in a single Activity.
is an Android application. Activities are covered in more depth in the following chapters.
The API Demos application, as shown in the following illustration, contains multiple, smaller, examples of different Android functions:
Some of the applications included in the API Demos sample include 3-D image transitions, list and progress dialog boxes, and a finger-painting demo.
The Android documentation is located in the Docs folder within the Android SDK at ../%sdk folder%/DOCS. The documentation that is supplied with the SDK includes steps on downloading and installing the SDK, “Getting Started” quick steps for developing applications, and package definitions. The documentation is in HTML format and can be accessed though the documentation.html file in the root of the SDK folder. The following illustration depicts the main page of the Android SDK documentation. You can navigate to all of the documentation that is included in the Android SDK by using the links within documentation.html.
As you are navigating the Android SDK, you may think some pages are mislinked or missing, because the right side of the screen may be blank when you
click some links. However, if you scroll down you will see that the pages are just misaligned. In working with the Android SDK, we have found that there are sections of the documentation that we refer to more than others. For me, the most valuable segments of the Android SDK documentation are as follows (as they appear in the navigation bar):
● Reference Information
● Class Index
● List of Permissions
● List of Resource Types
The Troubleshooting subsection of the documentation will be especially helpful as you are starting out. As you progress through the book and begin to develop your own Android applications, you will find that the Reference Information section of the documentation is more helpful. For example, while it would have little to no use to you now, the List of Permissions subsection will be very helpful to you when you progress to
the section of the book that deals with creating more complex applications. Take some time to familiarize yourself with the Android SDK documentation and the hundreds of documents that have been provided for you.
karate game Play Online 3 Foot Ninja, Bowja The Ninja 2, Climbing Ninja, Dragon Fist 3, Naruto Battle For Leaf Village, Ninja Hunter, Ninja Plus, Ninja Quest, Ninja Rampage, Ninja Rinseout, Ninja Survival, Notebook Ninja, Pirates Vs Ninjas, Samurai Master, Sinjid Shadow Of The Warrior, Straw Hat Samurai, Super Ninja Strike Visit to here www.ninjaruler.com
The Android SDK is downloaded in a simple zipped package The bulk of the Android SDK, in number of files, consists of documentation, with programming APIs, tools, and samples comprising the rest. This section provides a closer look at exactly what is included in the Android SDK.
extract the Android SDK to the Program Files folder, so that it would be easier to track. If you are having trouble finding the SDK because you used the default extraction setting, it should be in the following folder: /%downloadfolder%/android-sdk_m5-rc14_windows/android-sdk_m5-rc14_windows.
Navigate to the folder where you unpacked the Android SDK so that you can begin to explore the folder structure within. While there are a few files in the root folder, like android.jar (a compiled Java application containing the core SDK libraries and APIs) and some release notes, the remainder of the Android SDK is divided into three main folders: Docs Contains
Much of the documentation found in the Docs folder can also be found on the http://code.google.com/android Android development site.
● Samples Contains six sample applications that you can compile and test from within Eclipse
● Tools Contains all of the development, compilation, and debugging tools that you need throughout the development process of an Android application
The following sections discuss in a bit more detail what is included in each of the SDK folders. Each API demo is compiled and run to illustrate the capabilities of Android. Many of the tools are discussed and demonstrated in later chapters as you learn how to create and compile applications using the command-line options of Microsoft Windows and Linux.