Java 如何调试 Android 后台服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18004711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug an Android Background Service?
提问by pointnetdeveloper
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?
我一直在为包含后台服务的 Android 开发 PhoneGap 应用程序。我的问题是:如何调试此服务?是否可以使用 AVD 进行调试并逐步进行?或者我可以使用我自己的设备来实现吗?
Thanks!
谢谢!
采纳答案by androiddev19
Yes, it can be done using AVD or device. Check out http://www.helloandroid.com/tutorials/how-debug-serviceand Debugging a service.
是的,可以使用 AVD 或设备来完成。查看http://www.helloandroid.com/tutorials/how-debug-service和Debugging a service。
回答by androiddev19
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
}
回答by rahulserver
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.
具体到intellij Idea,虽然也可能有一个等效的eclipse解决方案,但我在Shishupal的答案中添加了一些对我有用的点,我们需要添加android.os.Debug.waitForDebugger();
服务代码。
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:
在 12.1.4 版本中,它看起来像这样:
回答by RowanX
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.
- 打开 SDK(您的 SDK 文件夹)>tools>lib>monitor-x86_64
- 打开您的监控工具并选择一个设备,您可以像在通常的调试器中一样查看日志记录和按标签搜索。
回答by auspicious99
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.
除此之外,如果你的后台服务在一个单独的进程中,那么当你按下debug
按钮开始调试你的后台服务时,你还需要等到后台服务启动,然后按下按钮附加到进程(见截图Android Studio 3.6.1 .. 它是按钮右侧的第三个debug
按钮)。您可以选择要附加到的进程,其中之一是后台服务的单独进程。
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.
- 如果没有 line
android.os.Debug.waitForDebugger();
,后台服务将直接启动而无需等待调试器附加。 - 即使使用了 line
android.os.Debug.waitForDebugger();
,您仍然需要将调试器附加到后台服务的进程中。 - 按下按钮附加到进程的时间,需要在后台服务启动之后(在等待的时候
android.os.Debug.waitForDebugger();
),否则进程将不存在,你将无法选择它。