Tutorials

Welcome to our Tutorials section! We are working hard to bring you high quality and interesting tutorials. Also check out the tutorials forum if you have any problems or want to make a request. Also, if the text in the code samples is too small, click here for an explanation.

How to set location on emulator

emulatorYou have two options if you would like to set the location on the emulator. For both options, you start with starting the emulator first.

Option 1: using DDMS

  • go to your android/tools directory, and launch the DDMS tool
  • select the Emulator Control Tab
  • fill the Location Controls fields with the longitude and latitude values
  • press the Send button

Option 2: using telnet

  • open a command shell
  • conect to the emulator with the command: telnet localhost <port>
  • to enter a fixed location execute the command:
    geo fix <longitude> <latitude> [<altitude>]

For more information visit Location and Maps on the Android Developers page.

How to debug a Service?


debugIntroduction

When you start a project with a service in it in debug mode, and you placed a breakpoint in a method of the service, the debugger will not stop. 

Solution

The solution that works in almost all situation is to wait declaratively in the code for the debugger to attach. To do this, you have to make a call to:

  1. android.os.Debug.waitForDebugger();

The breakpoint can be inserted at any line after this call. 

As a complete example, the SoftKeyboard tutorial is enhanced with the call above:

  1. public class SoftKeyboard extends InputMethodService
  2.         implements KeyboardView.OnKeyboardActionListener {
  3.        
  4.     @Override
  5.         public void onConfigurationChanged(Configuration newConfig) {
  6.         Log.d("SoftKeyboard", "onConfigurationChanged()");
  7.  
  8.         /* now let's wait until the debugger attaches */
  9.         android.os.Debug.waitForDebugger();
  10.        
  11.         super.onConfigurationChanged(newConfig);
  12.        
  13.         /* do something useful... */
  14.                
  15.         }

As you can see in the code, the first call in the method is a call to the logger, with which we can see in the Log output when our method was called. This can be an other way of tracking our method calls, without the need to stop at a breakpoint. But usually this is not enough for detailed debugging.
Then the second statement waits for the debugger to attach, as it is also noted in the comment. After this line the breakpoint can be inserted anywhere in the method.

Debugging a Service in case an Activity is also part of the application is even easier. In that case the activity has to be started first, then the debugger is able to stop at the breakpoint in the service as well, without the need of an explicit waitForDebugger() call.

TourGuide - Virtual Sightseeing

tourguide_m5.png

MusicDroid - Audio Player Part III

MusicDroid3.png

NewsDroid RSS Reader

NewsDroid1.png