19

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...

JDJ
  • 4,298
  • 3
  • 25
  • 44
Vikas Singh
  • 1,781
  • 7
  • 27
  • 54
  • 2
    If its not critical to be invoked at boot time you could simply call that method later, for testing. – Nappy Apr 10 '12 at 10:29

4 Answers4

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

Dimitri
  • 6,923
  • 4
  • 35
  • 49
fklappan
  • 3,259
  • 2
  • 17
  • 18
  • 1
    yes you can have a look at http://en.androidwiki.com/wiki/ADB_Shell_Command_Reference – Its not blank Apr 16 '12 at 12:13
  • Just a note, it's also ./adb shell in windows Powershell. Great answer thanks. – Mafro34 Nov 17 '13 at 21:20
  • 1
    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! – Dave Jan 29 '14 at 04:50
  • 10
    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. – Someone Somewhere Jul 06 '15 at 23:49
  • 6
    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. – Deathspike Aug 16 '16 at 09:21
  • 5
    I try this, but get "permission denial. not allowed to send broadcast ... from pid=.... uid=...." – Zordid Dec 29 '17 at 00:30
  • 1
    Security exception: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED – tim4dev Apr 14 '18 at 06:26
  • 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). – fklappan Apr 16 '18 at 10:59
  • For the "permission denial. not allowed to send broadcast" issue,, running "adb root" before running the command to broadcast BOOT_RECEIVED, works – Melvin Jan 08 '23 at 06:45
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.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
1

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

Its not blank
  • 3,055
  • 22
  • 37
  • 1
    I agree that is what we can do but my question is about how to debug such scenarios using debuggers? – Vikas Singh Apr 10 '12 at 10:48
  • 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. – Its not blank Apr 10 '12 at 10:54
1

Just put to your terminal in Android Studio

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

whalemare
  • 1,107
  • 1
  • 13
  • 30
  • Note that it is `android.intent.action.BOOT_COMPLETED` instead of `android.intent.action.BOOT_COMPLETE` (there is a `D` missing at the end) – Bennik2000 Oct 01 '21 at 14:55