Debugging Applications with DDMS
The Dalvik Debug Monitor Service (DDMS) is a debugging utility that is integrated into Eclipse through a special Eclipse perspective. The DDMS perspective provides a number of useful features for interacting with emulators and handsets and debugging applications (Figure 2.2).
Figure 2.2 The DDMS perspective, with one emulator and two Android devices connected (the Nexus S running 2.3.1 and the Samsung Galaxy Tablet running 2.2).
The features of DDMS are roughly divided into five functional areas:
- Task management
- File management
- Emulator interaction
- Screen captures
DDMS and the DDMS perspective are essential debugging tools. Now let's take a look at how to use these features in a bit more detail.
The top-left corner of the DDMS perspective lists the emulators and handsets currently connected. You can select individual instances and view its processes and threads. You can inspect threads by clicking on the device process you are interested in—for example, com.androidbook.droid1—and clicking the Update Threads button (), as shown in Figure 2.3. You can also prompt garbage collection on a process and then view the heap updates by clicking the Update Heap button (). Finally, you can stop a process by clicking the Stop Process button ().
Figure 2.3 Using DDMS to examine thread activity for the Droid1 application.
Browsing the Android File System
You can use the DDMS File Explorer to browse files and directories on the emulator or a device (Figure 2.4). You can copy files between the Android file system and your development machine by using the Push () and Pull () buttons available in the top right-hand corner of the File Explorer tab.
Figure 2.4 Using the DDMS File Explorer to browse system fonts on the handset.
You can also delete files and directories by using the Delete button () or just pressing the Delete key. There is no confirmation for this delete operation, nor can it be undone.
Interacting with Emulators
DDMS can send a number of events, such as simulated calls, SMS messages, and location coordinates, to specific emulator instances. These features are found under the Emulator Control tab in DDMS. These events are all "one way," meaning that they can be initiated from DDMS, not from the emulator to DDMS.
Simulating Incoming Calls to the Emulator
You can simulate incoming voice calls by using the DDMS Emulator Control tab (see Figure 2.5). This is not a real call; no data (voice or otherwise) is transmitted between the caller and the receiver.
Figure 2.5 Using the DDMS Emulator Control tab (left) to place a call to the emulator (right).
To simulate an incoming call to an emulator running on your machine, follow these steps:
- In the DDMS perspective, choose the emulator instance you want to call.
- On the Emulator Control tab, navigate to the Telephony Actions section and input the incoming number (for example, 5551212).
- Select the Voice radio button.
- Click the Call button.
- In the emulator, you should see an incoming call. Answer the call by clicking the Send button in the emulator or sliding the slider to the right.
- End the call at any time by clicking the End button in the emulator or by clicking the Hang Up button in the DDMS perspective.
Simulating Incoming SMS Messages to the Emulator
You can simulate incoming SMS messages by using the Emulator DDMS Emulator Control tab (see Figure 2.6). You send an SMS much as you initiate a voice call.
Figure 2.6 Using the DDMS Emulator Control tab (left) to send an SMS message to the emulator (right).
To send an SMS message to an emulator running on your machine, follow these steps:
- In the DDMS perspective, choose the emulator instance you want a send an SMS message to.
- On the Emulator Control tab, navigate to the Telephony Actions section and input the Incoming number (for example, 5551212).
- Select the SMS radio button.
- Type an SMS message in the Message textbox.
- Click the Send button. In the emulator, you should see an incoming SMS notification on the notification bar. Pull down the bar to view the SMS message details.
Taking Screenshots of the Emulator or Handset
One feature that can be particularly useful for debugging both handsets and emulators is the ability to take screenshots of the current screen (see Figure 2.7).
Figure 2.7 Using the DDMS Screen Capture button to take a screenshot of the Nexus S handset, which happens to be displaying some old photo albums in the Gallery.
The screenshot feature of the DDMS perspective is particularly useful when used with real devices. To take a screen capture of what's going on at this very moment on your device, follow these steps:
- In the DDMS perspective, choose the device (or emulator) you want a screenshot of. The device must be connected via USB.
- On that device or emulator, make sure you have the screen you want. Navigate to it, if necessary.
- Press the Screen Capture button () to take a screen capture. This launches a capture screen dialog.
- Within the capture screen, click the Save button to save the screenshot to your local hard drive. The Rotate button rotates the Device Screen Capture tool to display in landscape mode. This tool does not show a live view, just a snapshot; click the Refresh button to update the capture view if you make changes on the device. The Copy button places the image on your system's clipboard for pasting into another application, such as an image editor. Click the Done button to exit the tool and return to the DDMS perspective.
Viewing Log Information
The LogCat logging utility that is integrated into the DDMS perspective enables you to view the Android logging console. You might have noted the LogCat logging tab, with its diagnostic output, in Figure 2.2 earlier in this chapter. We talk more about how to implement your own custom application logging in Hour 3, "Building Android Applications."