139
Chapter 5: Coding Your Application
Figure 5-9:
A view of LogCat.
DDMS is also very useful in scenarios where you do not have an actual device to test with. A key example of this is that you might be developing
an application that uses GPS and a Google MapView to show a map on the screen. Your application is based on a user moving across a map. If you don’t
have a device that has GPS, or a device at all for that matter, this becomes a very nontrivial task Thankfully, DDMS is here to help. DDMS provides tools
through what’s known as location control. As a developer, you can manually provide GPS coordinates, or you can provide a GPS eXchange Format GPX
file or a Keyhole Markup Language KML file that represents points on a map that can be timed accordingly for example, stay at this point for 5 seconds,
go to this point, and then go to the next point, and so on.
I’m barely scratching the surface of DDMS and its feature set. I’m going to show you how to get messages into DDMS as well as how to view them from Eclipse.
How to get log messages into DDMS
Getting messages into DDMS is as simple as supplying one line of code. Open the MainActivity.java file, and at the bottom of the method, add a log
entry, as shown in bold in Listing 5-5.
Listing 5-5: The onCreate Method
Override public void onCreateBundle savedInstanceState {
super.onCreatesavedInstanceState; setContentViewR.layout.main;
mAudioManager = AudioManagergetSystemServiceAUDIO_SERVICE; checkIfPhoneIsSilent;
setButtonClickListener;
Log.d“SilentModeApp”, “This is a test”;
➝
12
}
140
Part II: Building and Publishing Your First Android Application
The code listed at
➝
12 demonstrates how to get a message into the system
log. SilentModeApp is known as the TAG that you’re giving this log entry; the second parameter to the log call is the message that you want to output.
The tag helps filter messages while looking at them in DDMS.
A good convention to follow is to declare a TAG constant in your code and use that instead of repeatedly typing the TAG. An example would be
private static final String TAG = “SilentModeApp”; Notice the d in Log.d in Listing 5-5. The d means that this is a debug
message. Other options are as follows: ✓
e: error. ✓
I: info. ✓
wtf: What a terrible error yes, I’m serious, it’s there. ✓
v: verbose. The various logging types exist for you to decide how various messages
should be logged. You have to import the android.util.Log package for logging to work.
How to view DDMS messages
You’re probably wondering how you can view the DDMS messages. You can view DDMS by either opening DDMS manually or by opening the DDMS
perspective in Eclipse:
✓ Manual method: Navigate to where you installed the Android SDK.
Inside the tools directory, double-click the ddms.bat file. This starts the DDMS application outside of the Eclipse IDE, as shown in Figure 5-10.
✓
In Eclipse: The ADT has installed a DDMS perspective. To open the DDMS perspective, click the Open Perspective button, as shown in
Figure 5-11, and choose DDMS. If DDMS is not visible in this view, select the Other option and then select DDMS. This adds a DDMS perspec-
tive to the list of perspectives that you can easily toggle between. You should have been automatically switched to the DDMS perspective.
You can view LogCat usually near the bottom of the screen. I prefer to move my LogCat window to the main area of the screen, as shown in
Figure 5-12. To move your LogCat window to this location, simply drag the LogCat tab title and drop it to the location you want.
141
Chapter 5: Coding Your Application