Writing Your First Android Application

Writing Your First Android Application

Testing Your Development Environment

The best way to make sure you configured your development environment correctly is to take an existing Android application and run it.You can do this easily by using one of the sample applications provided as part of the Android SDK in the /samples subdirectory. Within the Android SDK sample applications, you can find a classic game called Snake. To build and run the Snake application, you must create a new Android project in your Eclipse workspace, create an appropriate Android Virtual Device (AVD) profile, and configure a launch configuration for that project.After you have everything set up correctly, you can build the application and run it on the Android emulator or an Android device. Adding the Snake Application to a Project in Your Eclipse Workspace The first thing you need to do is add the Snake project to your Eclipse workspace.To do this, follow these steps: 1. Choose File, New, Project. 2. Choose Android,Android Project Wizard (see Figure 3.1). Writing Your First Android Application 44 Chapter 3 Writing Your First Android Application Tip After you use the Android Project wizard once, you can create subsequent projects using File, New, Android Project. 3. Change the Contents to Create Project from Existing Source. 4. Browse to your Android samples directory. 5. Choose the Snake directory.All the project fields should be filled in for you from the Manifest file (see Figure 3.2).You might want to set the Build Target to whatever version of Android your test device is running. 6. Choose Finish.You now see the Snake project files in your workspace (see Figure 3.3). Warning Occasionally Eclipse shows the error “Project ‘Snake’ is missing required source folder: ‘gen’” when you’re adding an existing project to the workspace. If this happens, simply navigate to the project file called R.java under the /gen directory and delete it. The R.java file is automatically regenerated and the error should disappear. 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

Creating a Launch Configuration for Your Snake

Project Next, you must create a launch configuration in Eclipse to configure under what circumstances the Snake application builds and launches.The launch configuration is where you configure the emulator options to use and the entry point for your application. Testing Your Development Environment 47 Figure 3.4 Creating a new AVD in Eclipse. You can create Run Configurations and Debug Configurations separately, each with different options.These configurations are created under the Run menu in Eclipse (Run, Run Configurations and Run, Debug Configurations). Follow these steps to create a basic Run Configuration for the Snake application: 1. Choose Run, Run Configurations (or right-click the Project and choose Run As). 2. Double-click Android Application. 3. Name your Run Configuration SnakeRunConfiguration (see Figure 3.5). 4. Choose the project by clicking the Browse button and choosing the Snake project. 5. Switch to the Target tab and, from the preferred AVD list, choose the Android_Vanilla2.2 AVD created earlier, as shown in Figure 3.5. You can set other options on the Target and Common tabs, but for now we are leaving the defaults as they are. Running the Snake Application in the Android Emulator Now you can run the Snake application using the following steps: 1. Choose the Run As icon drop-down menu on the toolbar (the green circle with the triangle). 2. Pull the drop-down menu and choose the SnakeRunConfiguration you created. 3. The Android emulator starts up; this might take a moment. 48 Chapter 3 Writing Your First Android Application Figure 3.5 The Snake project launch configuration, Target tab with the AVD selected. Tip Make sure you don’t have your Android device plugged in to your development machine via USB at this time. Automatic is the default selection in the Target tab of the Run Configuration for Device Target Selection mode, so Snake might launch on your device instead of within the emulator if the device is attached. 4. If necessary, swipe the screen from left to right to unlock the emulator, as shown in Figure 3.6. 5. The Snake application now starts, as shown in Figure 3.7. You can interact with the Snake application through the emulator and play the game.You can also launch the Snake application from the Application drawer at any time by clicking on its application icon.

Building Your First Android Application

Now it’s time to write your first Android application.You start with a simple Hello World project and build upon it to explore some of the features of Android. Tip 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. Building Your First Android Application 49 Figure 3.6 The Android emulator launching (locked). Figure 3.7 The Snake game. 50 Chapter 3 Writing Your First Android Application Creating and Configuring a New Android Project You can create a new Android project in much the same way as when you added the Snake application to your Eclipse workspace. The first thing you need to do is create a new project in your Eclipse workspace.The Android Project Wizard creates all the required files for an Android application. Follow these steps within Eclipse to create a new project: 1. Choose File, New,Android Project, or choose the Android Project creator icon, which looks like a folder (with the letter a and a plus sign), on the Eclipse toolbar. 2. Choose a Project Name. In this case, name the project MyFirstAndroidApp. 3. Choose a Location for the project files. Because this is a new project, select the Create New Project in Workspace radio button. Check the Use Default Location checkbox or change the directory to wherever you want to store the source files. 4. Select a build target for your application. Choose a target that is compatible with the Android handsets you have in your possession. For this example, you might use the Android 2.2 target. 5. Choose an application name.The application name is the “friendly” name of the application and the name shown with the icon on the application launcher. In this case, the Application Name is My First Android App. 6. Choose a package name. Here you should follow standard package namespace conventions for Java. Because all our code examples in this book fall under the com.androidbook.* namespace, we will use the package name com.androidbook.myfirstandroidapp, but you are free to choose your own package name. 7. Check the Create Activity checkbox.This instructs the wizard to create a default launch activity for the application.Call this Activity class MyFirstAndroidAppActivity.

Formation et coursTélécharger le document complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *