调试服务
我编写了一个带有远程接口的服务并将其安装在我的 PC 的 Eclipse AVD 上。我有一个客户端测试工具,它启动并调用服务中的方法。最初,我通过控制类和活动安装了该服务,现在我已将其删除,因此该服务的清单如下所示:
<?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>
因此没有活动标记。
当我从 Eclipse 中的调试图标启动它时,控制台告诉我它正在安装 apk(确实如此),但它不会显示为调试线程,并且不会触发断点,尽管服务的行为到目前为止还不错正如客户所见。如果我将服务标签包装在具有关联类的活动标签中并启动它,那么我就可以调试它。
是否可以在不将服务包装在活动中的情况下调试该服务?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过四个步骤执行以下操作:
First:在服务的第一个有趣的方法中(我在创建时使用):
Second:在
waitForDebugger
命令。第三:通过 IDE (Eclipse/Android Studio/...) 中的调试按钮启动应用程序。 (您现在应该已经从清单中删除了主要启动活动)
最后:启动 adb 并运行命令来启动服务:
cd $PLATFORM_TOOLS
adb shell
am startservice -n com.google.android.apps.gtalkservice/com.google.android.gtalkservice.service.GTalkService
Here's what you can do in four steps:
First: In the first interesting method of your service (I used on create):
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
只要确保您不要忘记代码中的这行代码并发布您的 apk 即可。 如果您尝试在没有调试器的情况下运行应用程序,下面的行将被卡住。
您还可以使用以下命令来确定调试器是否已连接:
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.
also you can use the following to determine if the debugger is connected:
编辑 2018 按钮图标已更改
这非常简单,您可以连接到您的应用程序服务。假设运行调试器不起作用,您可以通过按下带有箭头图标的调试来按下选择进程选项,如上图所示。选择您的服务,您现在应该能够调试您的服务。
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.
我认为应该使用 android.os.Debug.waitForDebugger(); 以编程方式完成
I think it should be done programmatically with android.os.Debug.waitForDebugger();
这适用于 Android Studio。我想 Eclipse 中可能有类似的方式。
This works in Android Studio. There might be a similar way in Eclipse I suppose.
一些答案正确地提到您想要插入
到服务的第一个有趣的方法中。然而,从这些答案中并不清楚,Android Studio 调试器在服务启动时不会自动启动。
相反,您还需要等到服务启动,然后按按钮附加到进程(请参阅Android Studio 3.6.1的屏幕截图..它是
debug
右侧的第三个按钮按钮)。您将可以选择要附加的进程,其中之一是服务的单独进程。然后,您可以选择它来完成将调试器附加到服务的过程。编辑,2020 年 8 月:按钮图标与 Android Studio 4.0 中的相同
Some of the answers correctly mention that you'd want to insert
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