Вопрос

I have been developing a PhoneGap application for Android which contains a Background Service. My question is: How can I debug this service? Is it possible to debug using an AVD and go step by step? or can I use my own device to achieve that?

Thanks!

Это было полезно?

Решение

Yes, it can be done using AVD or device. Check out http://www.helloandroid.com/tutorials/how-debug-service and Debugging a service.

Другие советы

You can debug your service by putting a single statement , I am mentioning it here :

    @Override
public void onCreate() {
super.onCreate();
//whatever else you have to to here...
android.os.Debug.waitForDebugger();  // this line is key
}

You can use your usual logs -for not in-depth debugging- inside the service and then use monitor tool to view those logs when the app is in the background or closed.

To open the monitor tool:

  1. Open SDK( your SDK folder ) >tools>lib>monitor-x86_64
  2. Open your monitor tool and select a device and you can view the loggings and search by tag as you do in your usual debugger.

Specific to intellij Idea, although there may be an equivalent solution for eclipse also, I am adding a few more points to Shishupal's answer which worked for me, We need to add android.os.Debug.waitForDebugger(); in the service code.

But along with above, we need to uncheck "Deploy Application" and select "Do not launch Activity".

In version 12.1.4 it looks like this:

Screenshot

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.

this is the button, with background highlight

when you hover over the button

NB:

  1. Without the line android.os.Debug.waitForDebugger();, the background service would just start without waiting for the debugger to attach.
  2. Even with the line android.os.Debug.waitForDebugger();, you still need to attach the debugger to the process of the background service.
  3. 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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top