Android Application Development - Part 2

Published on December 2016 | Categories: Documents | Downloads: 35 | Comments: 0 | Views: 293
of 9
Download PDF   Embed   Report

Comments

Content

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Getting started with Android--Part II
Gareth Branwyn 10/21/2010 7:36 AM EDT Gareth Branwyn
Here's an excerpt from Sam's Teach Yourself Android Application Development in 24 Hours. Representing Hour 1, this segment of the two-part series covers a the running and debugging of applications. To build and debug an Android application, you must first configure your project for debugging. The ADT plug-in enables you to do this entirely within the Eclipse development environment. Specifically, you need to do the following:

Configure an Android Virtual Device (AVD) for the emulator Create a debug configuration for your project Build the Android project and launch the debug configuration

When you have completed each of these tasks, Eclipse will attach its debugger to the Android emulator (or handset), and you are free to debug the application as needed.

Managing Android Virtual Devices To run an application in the Android emulator, you must configure an Android Virtual Device (AVD). The AVD profile describes the 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 so, its capacity. In this case, an AVD for the default installation of Android 2.1 will suffice. Here are the steps for creating a basic AVD:

1. Launch the Android SDK and AVD Manager from within Eclipse by clicking on the little green Android icon with the arrow

1 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

on the toolbar. You can also launch the manager by selecting Window, Android SDK and AVD Manager in Eclipse. 2. Click the Virtual Devices menu item on the left menu. The configured AVDs will be displayed as a list. 3. Click the New button to create a new AVD. 4. Choose a name for the AVD. Because you are going to take all the defaults, name this AVD VanillaAVD. 5. Choose a build target. For example, to support Android 2.1, choose the item build target called Android 2.1 - API Level 7 from the drop-down. 6. Choose an SD card capacity, in either kibibytes or mibibytes. This SD card image will take up space on your hard drive, so choose something reasonable, such as a 1024MiB or less. (The minimum is 9MiB, but keep in mind that the full size of the SD card is stored on your machine.) 7. Choose a skin. This option controls the different visual looks of the emulator. In this case, go with the default HVGA screen, which will display in portrait mode. Your project settings should look as shown in Figure 1.4. 8. Click the Create AVD button and wait for the operation to complete. 9. Click Finish.

Note: Because the Android Virtual Devices Manager formats the memory allocated for SD card images, creating AVDs with SD cards may take a few moments.

Creating Debug and Run Configurations in Eclipse You are almost ready to launch your application. You have one last task remaining: You need to create a Debug configuration (or a Run configuration) for your project. To do this, take the following steps:

1. In Eclipse, choose Run, Debug Configurations. 2. Double-click the Android Application item to create a new entry. 3. Choose that new entry, called New_configuration. 4. Change the name of the entry to DroidDebug. 5. Choose the Droid1 project by clicking the Browse button. 6. On the Target tab, check the box next to the AVD you created. 7. Apply your changes by clicking the Apply button. Your Debug Configurations dialog should look as shown in Figure 1.5.
2 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Figure 1.5 The DroidDebug, debug configuration in Eclipse.

Did you know? If you choose Manual on the Target tab, instead of choosing Automatic and selecting an AVD, you will be prompted to choose a target each time you launch this configuration. This is useful when you're testing on a variety of handsets and emulator configurations. See "Launching Android Applications on a Handset," later in this hour, for more information.

Launching Android Applications Using the Emulator It's launch time, and your droid is ready to go! To launch the application, you can simply click the Debug button from within the Launch Configuration screen, or you can do it from the project by clicking the little green bug icon:

on the Eclipse toolbar. Then select DroidDebug Debug Configuration from the list. The first time you try to select DroidDebug debug configuration from the little green bug drop-down, you have to navigate through the configuration manager. Future attempts will show this configuration for convenient use under the bug drop-down.

After you click the Debug button, the emulator screen will launch. This can take some time, so be patient. You may need to click the Menu button on the emulator when you come to the Screen Locked view (see Figure 1.6).

3 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

FIGURE 1.6 An Android emulator launching (Screen Locked view).

Now the Eclipse debugger is attached, and your application runs, as shown in Figure 1.7.

FIGURE 1.7 The Droid #1 Android application running in the emulator.

As you can see, the application is very simple. It displays a single TextView control, with a line of text. The application does nothing else.

4 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Debugging Android Applications Using DDMS In addition to the normal Debug perspective built into Eclipse for stepping through code and debugging, the ADT plug-in adds the DDMS perspective. While you have the application running, take a quick look at this perspective in Eclipse. You can get to the DDMS perspective (see Figure 1.8) by clicking the Android DDMS icon:

in the top-right corner of Eclipse. To switch back to the Eclipse Project Explorer, simply choose the Java perspective from the top-right corner of Eclipse.

If the DDMS perspective is not visible in Eclipse, you can add it to your workspace by clicking the Open Perspective button in the top right next to the available perspectives (or, alternately, choose Window, Open Perspective). To see a complete list of available perspectives, select the Other option from the Open Perspective drop-down menu. Select the DDMS perspective and press OK.

FIGURE 1.8 The DDMS perspective in Eclipse

The DDMS perspective can be used to monitor application processes, as well as interact with the emulator. You can simulate voice calls and send SMS messages to the emulator. You can send a mock location fix to the emulator to mimic location-based services. You will learn more about DDMS and the other tools available to Android developers in Hour 2, "Mastering the Android Development Tools." Getting started with Android--Part II The LogCat logging tool is displayed on both the DDMS perspective and the Debug Perspective. This tool displays logging information from the emulator or the handset, if a handset is plugged in.
5 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Launching Android Applications on a Handset It's time to load your application onto a real handset. To do this, you need to plug a handset into your computer, using the USB data cable.

To ensure that you debug using the correct settings, follow these steps:

1. In Eclipse, choose Run, Debug Configurations. 2. Double-click DroidDebug Debug Configuration. 3. On the Target tab, set Deployment Target Selection Mode to Manual. You can always change it back to Automatic later, but choosing Manual will force you to choose whether to debug within the emulator (and choose an AVD) or a device, if one is plugged in, whenever you choose to debug. 4. Apply your changes by clicking the Apply button. 5. Plug an Android device into your development computer, using a USB cable. 6. Click the Debug button within Eclipse. 7. Double-click one of the running Android devices. There should be one listed for each handset plugged into the machine, in addition to one for each emulator instance running. If you do not see the handset listed, check your cables and make sure you installed the appropriate drivers, as explained in Appendix A. A dialog (Figure 1.9) appears, showing all available configurations for running and debugging your application. All physical devices are listed, as are existing emulators that are running. You can also launch new emulator instances by using other AVDs you have created.

FIGURE 1.9 The Eclipse dialog for choosing an application deployment

6 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Eclipse now installs the Android application on the handset, attaches a debugger, and runs the application. Your handset now shows a screen very similar to the one you saw in the emulator. If you look at the DDMS perspective in Eclipse, you see that logging information is available, and many features of the DDMS perspective work with real handsets as well as the emulator.

New to Eclipse? If you're still learning the ropes of the Eclipse development environment, now is a great time to check out Appendix B, "Eclipse IDE Tips and Tricks."

Summary Congratulations! You are now an Android developer. You are starting to learn your way around the Eclipse development environment. You created your first Android project. You reviewed and compiled working Android code. Finally, you ran your newly created Android application on the Android emulator as well as on a real Android device.

Q&A Q. What programming languages are supported for Android development? A. Right now, Java is the only programming language fully supported forAndroid development. Other languages, such as C++, may be added in the future. Although applications must be Java, C and C++ can be used for certain routines that need higher performance by using the Android NDK. For more information about using the Android NDK, see http://developer.android.com/sdk/ndk. Q. Why would I want to create AVDs for Android 1.1 (or any older firmware) when newer versions of the Android SDK are available? A. Although handset firmware may be updated over-the-air, not every Android device will support every future firmware version. Check the firmware available on each of your target handsets carefully before choosing which version your application will support and be tested on. Q. The Android resource editors can be cumbersome for entering large amounts of data, such as many string resources. Is there any way around this? A. Android project files, such as the Android manifest, layout files, and resource values (for example, /res/values/strings.xml), are stored in specially formatted XML files. You can edit these files manually by clicking on the XML tab of the resource editor. We will talk more about the XML formats in Hour 4.

Workshop

7 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Quiz 1. Who are the members of the Open Handset Alliance? A. Handset manufacturers B. Wireless operators and carriers C. Mobile software developers D. All of the above 2. True or False: You can simply launch the Android emulator to use default settings right after the SDK is installed. 3. What is the most popular IDE for Android development? A. Eclipse B. IntelliJ C. Emacs 4. True or False: You can use Eclipse for handset debugging.

Answers 1. D. The Open Handset Alliance is a business alliance that represents all levels of the handset supply chain. 2. False. You must first create an AVD. 3. A. Eclipse is the most popular IDE for Android development. Other IDEs can be used, but they will not enable you to use the ADT plug-in that is integrated with Eclipse. 4. True. Eclipse supports debugging within the emulator and on the handset.

Exercises 1. Visit http://developer.android.com and take a look around. Check out the online Developer's Guide and reference materials. Check out the Community tab and considering signing up for the Android Beginners and Android Developers Google Groups. 2. Add more text to the Droid #1 application. To do this, first add another String resource to the strings.xml resource file and save this file. Next, use the layout resource editor to modify the main.xml layout file to add a second TextView control. Set the text attribute of the TextView control to your newly created String resource. Finally, rerun the application in the emulator to see the results. 3. Add to your Eclipse workspace one of the Android sample projects provided with the Android SDK. Browse through the project files and then create a run configuration and launch the sample application in the emulator.

8 of 9

10/28/2010 10:08 AM

http://www.eetimes.com/General/DisplayPrintViewContent?contentItemI...

Sams Teach Yourself Android Application Development in 24 Hours * By Lauren Darcey, Shane Conder * Published Jun 10, 2010 by Sams. o Copyright 2010 o Edition: 1st o eBook o ISBN-10: 0-13-262969-0 o ISBN-13: 978-0-13-262969-0 http://www.informit.com/store/product.aspx?isbn=0132629690

Reprinted with Permission of Pearson Education.

9 of 9

10/28/2010 10:08 AM

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close