Can't debug through onReceive() in boot completed receiver
Asked Answered
G

4

14

Thanks a ton to this site, I have made significant progress with my first Android project.

I'm trying to get the execution suspend in the onReceive() method of a boot completed receiver. Below are my manifest and receiver code.

Android 2.3.3
API - 10
IDE - Eclipse
Running on emulator

Manifest:

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo.notepad3" >

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application android:icon="@drawable/icon" >
    <activity
        android:label="@string/app_name"
        android:name=".ProjectTrackerHomeActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ProjectTrackerEditActivity" />

    <receiver android:name=".ProjectTrackerNotification" />

    <receiver
        android:name=".ProjectTrackerOnBootReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

</manifest>

Receiver:

public class ProjectTrackerOnBootReceiver extends BroadcastReceiver {
private ProjectTrackerDBAdapter mDbHelper;

@Override
public void onReceive(Context context, Intent intent) {
    Debug.waitForDebugger();
    AlarmManager
              mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

              //I place the break point at line 2, the alarm manager line

             // Further code, irrelevant
    }

My observations -
1. When I run this application in debug mode on eclipse, the break point is not even hit.
2. When I run some other application in debug mode, this break point is hit momentarily! But before I can proceed with a step by step execution, the execution resumes. It doesn't stop there.

My reasoning for this behavior is that -
1. When I run some other application, since this above app is already installed, it catches the boot complete broadcast and so the breakpoint is hit. (But why doesn't the execution halt at the breakpoint?)
2. When I run only this app, it gets installed first and in the time it takes for installation, it misses the boot complete broadcast.

May I please get some assistance with the below queries -
1. How can I make the execution halt at the breakpoint without it resuming further?
2. Can I somehow run an already installed version of this app on the emulator in debug mode "without having it to get freshly installed" on the emulator every time I run it?
3. Is there anything else I'm doing wrong or missing something?

Kindly let me know since I really need to debug through onReceive() to catch further application logic bugs. Thanks a lot, folks.

Geminate answered 16/11, 2011 at 2:57 Comment(0)
A
11

You need to shutdown the phone and start it up to ever see onReceive get called from bootcompleted. To debug this, just add a Log statement in onReceive instead of setting a breakpoint. Otherwise, you'll have to add some action to the receiver in the manifest and then manually sendBroadcast(new Intent("someName")) with the name you specified in the receiver element in the manifest.

Addicted answered 16/11, 2011 at 3:9 Comment(3)
Thanks a lot. sendBroadcast() helped me debug through the receiver :) Receiver class name : TrackerReceiver.java. Line in calling method : sendBroadcast(new Intent(getApplicationContext(), TrackerReceiver.class);Geminate
Thanks, simulating the "boot completed" using another broadcast event helps debugging most of basic issues.Lector
@BenjaminPiette haha good point. Could get very tiring to keep rebooting just to debug your onReceiveAddicted
D
12

Not sure why this question is asked so many times and it was so hard to find the answer but this works like a champ for me.

  1. In your receiver class within the onReceive() override add the below call for waitForDebugger() as the first call.
  2. Deploy to the device by running the app.
  3. Once the updated apk is on the device, reboot the device.
  4. While device is rebooting return to your IDE (In this example Android Studio 1.4) and go to "Run" menu > "Attach debugger to Android Process".
  5. Check "Show all processes".
  6. Wait... Once the waitForDebugger() line is hit, your app/package will appear in this list. In my case, it took at least 2 minutes AFTER the device rebooted.
  7. Click "OK", wait a moment and your breakpoint will be hit.

You can now step through your code like normal. Have fun!

@Override
public void onReceive(Context context, Intent intent) {

    // Add this at beginning of method
    android.os.Debug.waitForDebugger();

    // Place breakpoint below
    String myVariable = "Some Value";

}
Dina answered 24/10, 2015 at 1:52 Comment(2)
Excellent write up of the steps to debug, but unfortunately for me I never see my app listed in the prossessess available to run.Otranto
Worked for me. ThanksKendy
A
11

You need to shutdown the phone and start it up to ever see onReceive get called from bootcompleted. To debug this, just add a Log statement in onReceive instead of setting a breakpoint. Otherwise, you'll have to add some action to the receiver in the manifest and then manually sendBroadcast(new Intent("someName")) with the name you specified in the receiver element in the manifest.

Addicted answered 16/11, 2011 at 3:9 Comment(3)
Thanks a lot. sendBroadcast() helped me debug through the receiver :) Receiver class name : TrackerReceiver.java. Line in calling method : sendBroadcast(new Intent(getApplicationContext(), TrackerReceiver.class);Geminate
Thanks, simulating the "boot completed" using another broadcast event helps debugging most of basic issues.Lector
@BenjaminPiette haha good point. Could get very tiring to keep rebooting just to debug your onReceiveAddicted
S
4

Another method which I prefer as it means you do not need to keep restarting the device(boring, gradle takes long enough to load as it is), is to simply run the app in debug mode and then simulate an action which will call your Broadcast receiver using the following command in your terminal.

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.example.package_name

The above creates a broadcast with action "BOOT_COMPLETE" and now you can debug without restarting.

Selection answered 4/3, 2017 at 22:45 Comment(0)
W
3

Use the below line before the code where you want the debugger to stop:

android.os.Debug.waitForDebugger();
Warnke answered 22/1, 2014 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.