Joke Collection Website - Public benefit messages - How to debug services in Android development

How to debug services in Android development

How to debug services in Android development

In Android development, add code to debug services.

introduce

When starting an Android project in debugging mode, set a breakpoint in the service and the debugger will not stop.

solution

In all these cases, it is declared in the code. The method called is:

Android . OS . debug . waitfordebugger();

For example, soft keyboard:

The public class SoftKeyboard extends InputMethodService to realize KeyboardView. OnKeyboardActionListener { @ Override public void onConfigurationChanged(Co configuration new config){ log . d(" soft keyboard "," onConfigurationChanged()"); /* Now let's wait until the debugger attaches */android.os.debug.wait for debugger (); super . onconfigurationchanged(new config); /* Do something useful ... */}

As you can see in the code, the first thing is to call the recorder. When the code runs here, a record will be added to logcat, which is a way to track the code running. If it doesn't need to stop at a breakpoint, you can use it. But usually for more detailed debugging, this is not enough.

The second statement waits for the debugger to be added. After adding this statement, you can add a breakpoint anywhere in this method.

Debugging services is easier when the activity is also part of the application. In this case, you need to start the activity first, and the debugger can also stop at the breakpoint of the service without calling waitForDebugger ().