How to debug an Android Background Service?

2019-04-07 08:49发布

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!

4条回答
贪生不怕死
2楼-- · 2019-04-07 09:13

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

查看更多
3楼-- · 2019-04-07 09:21

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

查看更多
虎瘦雄心在
4楼-- · 2019-04-07 09:23

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
}
查看更多
孤傲高冷的网名
5楼-- · 2019-04-07 09:26

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.
查看更多
登录 后发表回答