How to debug an Android Background Service?

此生再无相见时 提交于 2019-12-18 16:48:30

问题


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:

  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.



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!