在Android Studio中调试服务的正确方法?

时间:2013-09-20 01:27:06

标签: android service android-studio

是否有正确的方法在Android Studio中调试服务?或Android Studio无法做到这一点?当我尝试调试时,它只会断开套接字。 (“与目标虚拟机断开连接,地址:'localhost:8600',传输'套接字')

希望你能在这里帮助我..

我在这里做了一些链接的建议,我放了.waitForDebugger但没有任何反应。请帮帮我..非常感谢。

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent mainService = new Intent(this,MainPowerAndVolumeService.class);

    startService(mainService);
    android.os.Debug.waitForDebugger();

    finish();

}

2 个答案:

答案 0 :(得分:12)

“在调试中运行”(在1.1.0版中看起来像这样:A phone icon with a small bug overlaid)旁边有一个按钮,用于将调试器附加到正在运行的android进程。这是官方信息(对于IntelliJ,但在Android Studio中是相同的): http://blog.jetbrains.com/idea/2011/05/new-in-105-attach-debugger-to-a-running-android-process/

答案 1 :(得分:0)

(于2020年8月更新:Android Studio 4.0中的按钮具有相同的图标)

因此,一旦服务启动,它正在等待调试器附加,但是您仍然需要告诉Android Studio将其附加到进程(它不会自动发生)。

按下按钮即可附加到进程(请参阅Android Studio 3.6的屏幕截图。它是debug按钮右侧的第三个按钮)。您可以选择要附加的流程,其中之一是后台服务的独立流程。

this is the button, with background highlight

按下按钮以附加到进程的时间必须在启动后台服务之后(等待android.os.Debug.waitForDebugger();时),否则该进程将不存在并且您将不会能够选择它。

相关问题