问题
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!
回答1:
Yes, it can be done using AVD or device. Check out http://www.helloandroid.com/tutorials/how-debug-service and Debugging a service.
回答2:
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
}
回答3:
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:
- Open SDK( your SDK folder ) >tools>lib>monitor-x86_64
- 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.
回答4:
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:

来源:https://stackoverflow.com/questions/18004711/how-to-debug-an-android-background-service