调试服务

发布于 2024-09-29 04:07:46 字数 1064 浏览 4 评论 0原文

我编写了一个带有远程接口的服务并将其安装在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空气里的味道 2024-10-06 04:07:46

您可以通过四个步骤执行以下操作:

First:在服务的第一个有趣的方法中(我在创建时使用):

/* (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:在 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):

/* (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
请恋爱 2024-10-06 04:07:46

只要确保您不要忘记代码中的这行代码并发布您的 apk 即可。 如果您尝试在没有调试器的情况下运行应用程序,下面的行将被卡住。

android.os.Debug.waitForDebugger();

您还可以使用以下命令来确定调试器是否已连接:

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

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.
橙味迷妹 2024-10-06 04:07:46

输入图片此处描述

编辑 2018 按钮图标已更改

在此处输入图像描述

在此处输入图像描述

这非常简单,您可以连接到您的应用程序服务。假设运行调试器不起作用,您可以通过按下带有箭头图标的调试来按下选择进程选项,如上图所示。选择您的服务,您现在应该能够调试您的服务。

enter image description here

Edit 2018 button icon changed

enter image description here

enter image description here

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.

千秋岁 2024-10-06 04:07:46

我认为应该使用 android.os.Debug.waitForDebugger(); 以编程方式完成

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

苯莒 2024-10-06 04:07:46

这适用于 Android Studio。我想 Eclipse 中可能有类似的方式。

  1. 在调试模式下运行您的项目
  2. 将调试器附加到您的服务进程

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

  1. run your project in debug mode
  2. attach debugger to your service process
我们只是彼此的过ke 2024-10-06 04:07:46

一些答案正确地提到您想要插入

android.os.Debug.waitForDebugger(); 

到服务的第一个有趣的方法中。然而,从这些答案中并不清楚,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

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.

this is the button, with background highlight

when you hover over the button

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文