Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 926
Nitin Avatar asked Nov 16 '11 02:11

Nitin


3 Answers

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.

like image 68
LifeQuestioner Avatar answered Nov 04 '22 13:11

LifeQuestioner


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";

}
like image 33
johnw182 Avatar answered Nov 04 '22 13:11

johnw182


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.

like image 23
LuxuryMode Avatar answered Nov 04 '22 14:11

LuxuryMode