I have written a service with a remote interface and installed it on my PC\'s Eclipse AVD. I have a client test harness which starts and invokes methods in the service. Init
Edit 2018 button icon changed
This is pretty simple, you can connect to your application service. Assuming that running the debugger doesn't work, you can press the choose process option by pressing the debug with an arrow icon pictured above. Select your service and you should now be able to debug your service.
just make sure you don't forget this line 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 you can use the following to determine if the debugger is connected:
android.os.Debug.isDebuggerConnected(); //Determine if a debugger is currently attached.
This works in Android Studio. There might be a similar way in Eclipse I suppose.
Here's what you can do in four steps:
First: In the first interesting method of your service (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 after the waitForDebugger
command.
Third: Launch app via debug button in your IDE (Eclipse/Android Studio/...). (You should probably have removed the main launch activity from the manifest by now)
Last: Launch 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
I think it should be done programmatically with android.os.Debug.waitForDebugger();
Some of the answers correctly mention that you'd want to insert
android.os.Debug.waitForDebugger();
into the first interesting method of the service. However, it's not clear from those answers, that the Android Studio debugger will not start automatically when the service is started.
Instead, you also need to wait till the service has started, then press the button to attach to process (see screenshot for Android Studio 3.6.1 .. it is the 3rd button to the right from the debug
button). You will be given a choice of processes to attach to, one of which would be the service's separate process. You can then select it to complete the process of attaching the debugger to the service.
Edit, Aug 2020: the button icon is the same in Android Studio 4.0