Quick Tip #001 — Debugging BroadcastReceiver
Last week we came across a bug in our application Fleek regarding our push notifications. The deeplink to a specific screen when clicking on the notification only worked in case the application was running in the background. If not, the application would just start the usual way after clicking on the notification without redirecting. The problem we faced while debugging was to attach a debugger in the case where the application is NOT running in the background as it seemed none of us ever faced that problem before. Shortly after I found online how you can do it (and it is very very simple) and wanted to share it with all the people that are just as clueless as I was.
Wait for debugger
There’s a way (actually there are two) to tell your application to wait for the debugger to be attached on startup. That way no code will be run until you attach the debugger manually within Android Studio. So how do you tell your app to wait for the debugger?
From within your code
Just add android.os.Debug.waitForDebugger();
.
From device settings
Go to Developer options
and under Debugging
click on Select debug app
and choose your application and enable the Wait for debugger option
like this:
So now every time the application starts (BroadcastReceivers included) you will see this lovely screen:
Just attach the debugger from within Android Studio and you can happily debug to your hearts content.
I hope this will be helpful to someone out there.
Until then…