[ACCEPTED]-Debugging a service-android-manifest

Accepted answer
Score: 92

Here's what you can do in four steps:

First: In 7 the first interesting method of your service 6 (I used on create):

/* (non-Javadoc)    
 * @see android.app.Service#onCreate()
 */
@Override
public void onCreate() {
    super.onCreate();
    //whatever else you have to to here...
    android.os.Debug.waitForDebugger();  // this line is key
}

Second: Set break points anywhere 5 after the waitForDebugger command.

Third: Launch app via debug 4 button in your IDE (Eclipse/Android Studio/...). (You 3 should probably have removed the main launch 2 activity from the manifest by now)

Last: Launch 1 adb and run the command to start a service:

  • cd $PLATFORM_TOOLS
  • adb shell
  • am startservice -n com.google.android.apps.gtalkservice/com.google.android.gtalkservice.service.GTalkService
Score: 23

just make sure you don't forget this line 3 of code in your code and release your apk. if you try running your app without the debugger the line below will get stuck.

android.os.Debug.waitForDebugger();

also 2 you can use the following to determine if 1 the debugger is connected:

android.os.Debug.isDebuggerConnected(); //Determine if a debugger is currently attached.
Score: 21

enter image description here

Edit 2018 button icon changed

enter image description here

enter image description here

This is pretty 7 simple, you can connect to your application 6 service. Assuming that running the debugger 5 doesn't work, you can press the choose process 4 option by pressing the debug with an arrow 3 icon pictured above. Select your service 2 and you should now be able to debug your 1 service.

Score: 5

I think it should be done programmatically 1 with android.os.Debug.waitForDebugger();

Score: 2

This works in Android Studio. There might 1 be a similar way in Eclipse I suppose.

  1. run your project in debug mode
  2. attach debugger to your service process
Score: 2

Some of the answers correctly mention that 16 you'd want to insert

android.os.Debug.waitForDebugger(); 

into the first interesting 15 method of the service. However, it's not 14 clear from those answers, that the Android 13 Studio debugger will not start automatically 12 when the service is started.

Instead, you 11 also need to wait till the service has started, then 10 press the button to attach to process (see 9 screenshot for Android Studio 3.6.1 .. it 8 is the 3rd button to the right from the 7 debug button). You will be given a choice of 6 processes to attach to, one of which would 5 be the service's separate process. You can 4 then select it to complete the process of 3 attaching the debugger to the service.

this is the button, with background highlight

when you hover over the button

Edit, Aug 2020: the 2 button icon is the same in Android Studio 1 4.0

More Related questions