Look for where your background service starts, for example
public BGcheckService() {
Log.d("BGcheckService: ", "starting service");
}
Then insert one line:
public BGcheckService() {
android.os.Debug.waitForDebugger();
Log.d("BGcheckService: ", "starting service");
}
Besides this, if your background service is in a separate process, then when you press the debug
button to start debugging your background service, you also need to wait till the background 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 background service's separate process.


NB:
- Without the line
android.os.Debug.waitForDebugger();
, the background service would just start without waiting for the debugger to attach.
- Even with the line
android.os.Debug.waitForDebugger();
, you still need to attach the debugger to the process of the background service.
- 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.