为什么要等待调试器,然后将调试器附加到进程中?

时间:2019-09-24 09:44:23

标签: android android-service android-testing android-debug android-developer-api

什么是

android.os.Debug.waitForDebugger();

我知道我们[有时]需要使用this function来调试Service,但是我的问题是:

  • 为什么,我们必须这样做吗?
  • 为什么这种方法存在?它还有其他目的吗?
  • attach a debugger to a process 是什么意思,为什么我们必须这样做?为什么必须以这种方式调试Service而不是ActivityBroadcastReceiver

人们会认为调试项目中的任何类型的代码都非常简单。除非不是。无论如何,Service都不适用。

此特殊程序存在的原因是什么?还有什么用?

enter image description here

  

等待直到附加调试器。调试器附加后,它将立即返回,因此,如果要立即开始跟踪,则需要在waitForDebugger()调用之后放置一个断点。

为什么仍然没有答案。

更新

我发现了此功能的一个用例:杀死某个应用程序进程后自动重新启动一个Android应用程序。当用户在应用程序设置中切换运行时权限时,该进程将被终止。更普遍的是,只要...,应用程序进程就会被终止并重新启动。

android.os.Process.killProcess(android.os.Process.myPid());

被称为。

要在进程重新启动后调试应用程序,请在代码中写入android.os.Debug.waitForDebugger(),然后将调试器附加到当前进程。

Android: how to debug app's start-up process

这是此功能的一个用例。

  

参考

Debugging a service

A proper way to Debug a Service in Android Studio?

Breakpoint in service not working

How to debug an Android Background Service?

How to debug a Service?

How to Attach debbuger to process from the first line without compile code every time

you can attach a debugger after the app is already open

  

可能相关

Accessibility service disabled upon each debug run

When using the Java debugger in Intellij what does “Drop Frame” mean?

Configure on-device developer options

Test your service

Force application to restart from first Activity

1 个答案:

答案 0 :(得分:4)

我不确定我的答案是正确的,因为您可能期望内部有复杂的见解。实际上,有一个非常简单的过程来连接和拦截过程响应。

enter image description here

  • 为什么存在此方法?它还有其他目的吗?

如您所见,回答这个问题非常简单。由于进程拦截期间的竞争状况,您将无法在您的早期中断点停止,应致电waitForDebugger()。在最常见的地方,例如Service调试和Application

  • “将调试器附加到进程”是什么意思,为什么我们必须这样做?为什么必须以这种方式调试服务,而不是Activity或BroadcastReceiver?

这是另一个问题,但是我不确定这里的上下文。我们需要调用该方法,并针对情况(例如,我们的Service使用清单中的另一个流程)手动附加到流程。显然调试桥将在应用程序的当前活动进程上设置,但是由于我们可以从另一个进程运行Service,因此我们需要将其手动添加到调试桥并等待调试连接。