Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a service

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. Initially I had the service installed by a control class and activity, which I have now removed, so that the manifest for the service looks like:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myname.gridservice" android:versionCode="1" android:versionName="1.0"> <application     android:icon="@drawable/icon"     android:label="@string/app_name"     android:debuggable="true">     <service         android:enabled="true"         android:debuggable="true"         android:name="OverlayService">         <intent-filter>             <action android:name="com.myname.OverlayService.SERVICE"/>             <action android:name="com.myname.gridservice.IRemoteInterface" />         </intent-filter>     </service>  </application>   </manifest>    

so there's no activity tag.

When I launch it from the debug icon in Eclipse, the console tells me that it's installing the apk (which it is), but it does not appear as a debug thread and breakpoints aren't triggered, although the service's behaviour is OK as far as the client sees it. If I wrap the service tag in an activity tag which has an associated class and launch that, then I can debug it.

Is it possible to debug the service without wrapping it in an activity?

like image 448
NickT Avatar asked Oct 24 '10 11:10

NickT


People also ask

How do I debug a Windows Net service?

Start the service (you can use net start , or start it in the Services window). You should see a dialog box like the following: Select Yes, debug <service name>. In the Just-In-Time Debugger window, select the version of Visual Studio you want to use for debugging.


2 Answers

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
like image 94
Brian Sweeney Avatar answered Oct 31 '22 20:10

Brian Sweeney


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. 
like image 33
Gilson Avatar answered Oct 31 '22 18:10

Gilson