A proper way to Debug a Service in Android Studio?
Asked Answered
U

2

14

Is there a proper way to debug a service in Android Studio? Or Android Studio could not do this? When I try to debug, it will just disconnects the sockets. ("Disconnected from the Target VM, address:'localhost:8600',transport 'socket')

Hopefully you can assist me here..

What I have done as advice by some links here, I had place the .waitForDebugger but nothing happens. Please help me.. Thank you very much.

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

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

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

    finish();

}
Ur answered 20/9, 2013 at 1:27 Comment(2)
similar problem here. It disconnects after about 30 seconds of idle time. I'm debugging a service.Aalto
Does this answer your question? debug background service in android using intelliJAttune
W
17

There is a button next to "run in debug" (In version 1.1.0 looks like this: A phone icon with a small bug overlaid) to attach the debugger to a running android process. Here is the official info (for IntelliJ, but it's the same in Android Studio): http://blog.jetbrains.com/idea/2011/05/new-in-105-attach-debugger-to-a-running-android-process/

Wimple answered 27/9, 2013 at 11:16 Comment(2)
Thank for the information. I'm testing it first for sudden disconnection.Ur
That's very nice, but how do you get it to run in the first place ? Using the adb shell to install and start the service manually everytime you change a byte in the code is a pain in the ass.Defrost
A
1

(updated in August 2020: the button in Android Studio 4.0 has the same icon)

So it is waiting for the debugger to attach, once the service has started, but you still need to tell Android Studio to attach to the process (it doesn't happen automatically).

Press the button to attach to process (see screenshot for Android Studio 3.6 .. 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 background service's separate process.

this is the button, with background highlight

The timing of when you press the button to attach to the process, needs to be after the background service has started (while it is waiting at android.os.Debug.waitForDebugger();), otherwise the process would not exist and you would not be able to select it.

Aubyn answered 29/5, 2020 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.