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.
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.
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.
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";
}
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With