How to debug a Service?
Introduction
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.
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:
- 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:
- public class SoftKeyboard extends InputMethodService
- implements KeyboardView.OnKeyboardActionListener {
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- Log.d("SoftKeyboard", "onConfigurationChanged()
"); - /* now let's wait until the debugger attaches */
- android.os.Debug.waitForDebugger();
- super.onConfigurationChanged(newConfig);
- /* do something useful... */
- }
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.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 5 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 5 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 5 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 5 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 5 days ago
Poll
Useful resources
Android Development Projects
- Simple Android App by nordicisg
- An Android app for designing an article by tzur87
- Augmented Reality Metaio/Junaio Geo-AR by zakazkydotbiz
- Clock Application by innovativedir
- Mobile Application by nasirovemin
- Modify an existing android project to produce a similar clone (source provided) by causalsolut
- Mobile Game by jack9w9
- Android: Simple web browser app with the accelerometer by crowder
- Real Estate Mobile App for iPhone, iPad, and android by jjones1515
- fix android bug by neshgold99



