How to debug BOOT_COMPLETE broadcast receiver's "Force Close" crashes?
Asked Answered
J

4

20

Since the phone restarts and thus gets disconnected from the Eclipse debugger/LogCat while it's booting up, how do I see where my boot complete broadcast receiver is crashing?

I am performing some actions in the onReceive() of my public class BootCompleteReceiver extends BroadcastReceiver { ... }

This is crashing and popping up a force close dialog when the phone boots. How do I debug this and see where the problem is?

The question holds true for debugging any BOOT_COMPLETE broadcast receivers.

Thanks!

EDIT

Yes, we can see the system logs in LogCat as the phone is booting up but my app Log.d(TAG,"Boot completed") has to wait till it (onReceive) gets triggered but by that time the app crashes because the problem is somewhere in the receiver itself. The app crashes before I can log anything. Also, I cannot use "Run in Debug mode" for a phone that's restarting...

Jackstraw answered 10/4, 2012 at 10:26 Comment(1)
If its not critical to be invoked at boot time you could simply call that method later, for testing.Krefetz
B
1

check your Intent's actions and bundles you are recieving ,they may null and can be a null pointer exception.

Boor answered 10/4, 2012 at 10:44 Comment(2)
I agree that is what we can do but my question is about how to debug such scenarios using debuggers?Jackstraw
cannot be done since adb get restarted as well and hence you cannot debug this. your debug starts only when adb starts but till then your reciever crashes.Tell me after trying this scenario.Boor
S
80

As i wrote on another thread:

You can emulate all broadcast actions by connecting via adb to the device and open a device shell.

Here we go:

  • open console/terminal and navigating to /platform-tools
  • type "adb shell" or on linux/mac "./adb shell"
  • in the shell type "am broadcast -a android.intent.action.BOOT_COMPLETED" or whatever action you want to fire.

In this way you should be able to debug.

There are a bunch of nice commands coming with adb or the adb shell. Just try it

Regards Flo

EDIT: Using the above method will also reboot the device. To prevent the device from rebooting use am broadcast -a android.intent.action.BOOT_COMPLETED com.example.app. Note the suffix with the application package name to which the broadcast goes. This enables you to send the BOOT_COMPLETED intent to ONLY YOUR app for debugging purposes. – Roel van Uden

Shem answered 11/4, 2012 at 11:39 Comment(9)
yes you can have a look at en.androidwiki.com/wiki/ADB_Shell_Command_ReferenceBoor
Just a note, it's also ./adb shell in windows Powershell. Great answer thanks.Weissman
Really good answer. I would accept this one if I could. One thing to note too, I sometimes had issues with log statements reporting to logcat on boot, but using an emulator I was able to test perfectly and get the appropriate logs. Thanks!Cathiecathleen
typing /Android/sdk/platform-tools/adb shell am broadcast -a android.intent.action.BOOT_COMPLETED causes the phone to actually reboot :-( hence, debugger is disconnected.Brundisium
I solved this using am broadcast -a android.intent.action.BOOT_COMPLETED com.example.app. Note the suffix with the application package name to which the broadcast goes. This enables you to send the BOOT_COMPLETED intent to ONLY YOUR app for debugging purposes.Quicklime
I try this, but get "permission denial. not allowed to send broadcast ... from pid=.... uid=...."Shayna
Security exception: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETEDAdelleadelpho
Which api level do you use? So we can edit the answer up to which android version the answer is valid. I think the answer is no longer valid for newer android versions... (i used android 4 if i mind right).Shem
For the "permission denial. not allowed to send broadcast" issue,, running "adb root" before running the command to broadcast BOOT_RECEIVED, worksPika
K
2

The receiver is only controlling when your code runs (i.e when the phone starts). Whilst debugging, run the code manually. You can resolve 99% of your issues this way and the remaining ones (if any) you can resolve by writing to LogCat so see what your code is doing.

Kaja answered 10/4, 2012 at 10:30 Comment(1)
Any ideas why logcat is not showing anything from my boot receiver class?Jetsam
B
1

check your Intent's actions and bundles you are recieving ,they may null and can be a null pointer exception.

Boor answered 10/4, 2012 at 10:44 Comment(2)
I agree that is what we can do but my question is about how to debug such scenarios using debuggers?Jackstraw
cannot be done since adb get restarted as well and hence you cannot debug this. your debug starts only when adb starts but till then your reciever crashes.Tell me after trying this scenario.Boor
T
1

Just put to your terminal in Android Studio

adb shell am broadcast -a android.intent.action.BOOT_COMPLETE

Theola answered 19/4, 2017 at 10:6 Comment(1)
Note that it is android.intent.action.BOOT_COMPLETED instead of android.intent.action.BOOT_COMPLETE (there is a D missing at the end)Kitts

© 2022 - 2024 — McMap. All rights reserved.