What is the significance of
android.os.Debug.waitForDebugger();
I am aware that we [sometimes] need to use this function in order to debug a Service
, but my question is:
- WHY do we have to do this?
- Why does this method exist? What other purpose does it serve?
- What does it mean to attach a debugger to a process, and why do we have to do this? Why does a
Service
have to be debugged in this way, and not anActivity
orBroadcastReceiver
?
One would think that debugging any kind of code in a project would be straightforward. Except it isn't. Not for a Service
anyway.
What is the reason for the existence of this special procedure? And what else is it used for?
Wait until a debugger attaches. As soon as the debugger attaches, this returns, so you will need to place a breakpoint after the waitForDebugger() call if you want to start tracing immediately.
The WHY remains unanswered.
UPDATE:
I found one use case for this function: when an Android app is auto-restarted after the app process is killed. The process is killed when a runtime permission is toggled by the user in the app settings. More generally, an app process is killed & restarted whenever ...
android.os.Process.killProcess(android.os.Process.myPid());
is called.
To debug the app after process restart, you write android.os.Debug.waitForDebugger()
in your code and then attach the debugger to the current process.
Android: how to debug app's start-up process.
This is one use case for this function.
References:
A proper way to Debug a Service in Android Studio?.
Breakpoint in service not working.
How to debug an Android Background Service?.
How to Attach debbuger to process from the first line without compile code every time.
you can attach a debugger after the app is already open.
Possibly related:
Accessibility service disabled upon each debug run
When using the Java debugger in Intellij what does “Drop Frame” mean?
android.os.Debug.waitForDebugger()
in order to debugService
's code? – PressmarkonStartCommand
and calledstartService
and the debugger stopped on that breakpoint - if you are using services that are running in the different process than original package then write it explicitly in your question - normal services work with no problems – Pressmarkandroid:process=..
attribute in theAndroidManifest.xml
file? – Pressmark