Выбрать главу

Figure 37-7. DDMS initial view

Clicking on an emulator allows you to browse the event log on the bottom and manipulate the emulator via the tabs on the right as shown in Figure 37-8.

Figure 37-8. DDMS, with emulator selected

Logging

Rather than use adb logcat, DDMS lets you view your logging information in a scrollable table. Just highlight the emulator or device you want to monitor, and the bottom half of the screen shows the logs (see Figure 37-9).

Figure 37-9. DDMS logging filter

In addition, you can:

• Filter the Log tab by any of the five logging levels, shown as the V through E toolbar buttons.

• Create a custom filter, so you can view only those tagged with your application’s tag, by pressing the + toolbar button and completing the form. The name you enter in the form will be used as the name of another logging output tab in the bottom portion of the DDMS main window.

• Save the log information to a text file for later perusal, or for searching.

File Push and Pull

While you can use adb pull and adb push to get files to and from an emulator or device, DDMS lets you do that visually. Just highlight the emulator or device you wish to work with, then choose Device→File Explorer… from the main menu. That will bring up the typical directory browser seen in Figure 37-10.

Figure 37-10. DDMS File Explorer

Just browse to the file you want and click either the pull (left-most) or push (middle) toolbar button to transfer the file to/from your development machine. Or, click the delete (right-most) toolbar button to delete the file.

There are a few caveats to this:

• You cannot create directories through this tool. You will either need to use adb shell or create them from within your application.

• While you can putter through most of the files on an emulator, you can access very little outside of /sdcard on an actual device, due to Android security restrictions.

Screenshots

To take a screenshot of the Android emulator or device, simply press <Ctrl>-<S> or choose Device→Screen capture… from the main menu. This will bring up a dialog box containing an image of the current screen shown in Figure 37-11.

Figure 37-11. DDMS screen capture

From here, you can click Save to save the image as a PNG file somewhere on your development machine, Refresh to update the image based on the current state of the emulator or device, or Done to close the dialog.

Location Updates

To use DDMS to supply location updates to your application, the first thing you must do is have your application use the gps LocationProvider, as that is the one that DDMS is set to update.

Then, click on the Emulator Control tab and scroll down to the Location Controls section. Here, you will find a smaller tabbed pane with three options for specifying locations: Manual, GPX, and KML (see Figure 37-12).

Figure 37-12. DDMS location controls

The Manual tab is fairly self-explanatory: provide a latitude and longitude and click the Send button to submit that location to the emulator. The emulator, in turn will notify any location listeners of the new position.

Discussion of the GPX and KML options is beyond the scope of this book.

Placing Calls and Messages

If you want to simulate incoming calls or SMS messages to the Android emulator, DDMS can handle that as well.

On the Emulator Control tab, above the Location Controls group, is the Telephony Actions group (see Figure 37-13).

Figure 37-13. DDMS telephony controls

To simulate an incoming call, fill in a phone number, choose the Voice radio button, and click Call. At that point, the emulator will show the incoming call, allowing you to accept it (via the green phone button) or reject it (via the red phone button) seen in Figure 37-14.

Figure 37-14. Simulated incoming call

To simulate an incoming text message, fill in a phone number, choose the SMS radio button, enter a message in the provided text area, and click Send. The text message will then appear as a notification as shown in Figure 37-15.

Figure 37-15. Simulated text message

Of course, you can click on the notification to view the message in the full-fledged Messaging application as you can see in Figure 37-16.

Figure 37-16. Simulated text message, in Messaging application

Put it On My Card

The T-Mobile G1 has a microSD card slot. Many other Android devices are likely to have similar forms of removable storage, which the Android platform refers to generically as an “SD card”.

SD cards are strongly recommended to be used by developers as the holding pen for large data sets: images, movie clips, audio files, etc. The T-Mobile G1, in particular, has a relatively paltry amount of on-board flash memory, so the more you can store on an SD card, the better.

Of course, the challenge is that, while the G1 has an SD card by default, the emulator does not. To make the emulator work like the G1, you need to create and “insert” an SD card into the emulator.

Creating a Card Image

Rather than require emulators to somehow have access to an actual SD card reader and use actual SD cards, Android is set up to use card images. An image is simply a file that the emulator will treat as if it were an SD card volume. If you are used to disk images used with virtualization tools (e.g., VirtualBox), the concept is the same: Android uses a disk image representing the SD card contents.

To create such an image, use the mksdcard utility, provided in the tools/ directory of your SDK installation. This takes two main parameters:

1. The size of the image, and hence the size of the resulting “card.” If you just supply a number, it is interpreted as a size in bytes. Alternatively, you can append K or M to the number to indicate a size in kilobytes or megabytes, respectively.

2. The filename under which to store the image.

For example, to create a 1GB SD card image, to simulate the G1’s SD card in the emulator, you could run:

mksdcard 1024M sdcard.img

Inserting the Card

To have your emulator use this SD card image, start the emulator with the -sdcard switch, containing a fully-qualified path to the image file you created using mksdcard. While there will be no visible impact — there is no icon or anything in Android showing that you have a card mounted — the /sdcard path will now be available for reading and writing.