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

Nitin picture Nitin · Nov 16, 2011 · Viewed 7.5k times · Source

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.

Answer

LuxuryMode picture LuxuryMode · Nov 16, 2011

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.