Debugging BOOT_COMPLETE Broadcast Receivers

5:09 AM 0 Comments

This post explains how to catch and debug the system broadcast BOOT_COMPLETED, after the system has finished booting.

Sometimes we want to start our applications on boot. BroadcastReceiver has trick for that. After the system has finished booting, the BroadcastReceiver is receiving an Intent BOOT_COMPLETED broadcast and perform our application-specific initialization, such as installing alarms.

Steps to catch the system bootup:

1. Get user permission
2. Create MainActivity and BroadcastReceiver class
3. Receive BOOT COMPLETED event
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1"
          android:versionName="1.0"
          package="com.test.android.boot"
          xmlns:android="http://schemas.android.com/apk/res/android">
          
  <uses-sdk android:minSdkVersion="3"
            android:targetSdkVersion="16" />
 
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <application android:icon="@drawable/cw"
               android:label="@string/app_name">
      
  <activity
            android:name="com.test.android.boot.TestActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  
    <receiver android:name=".MyBootReceiver">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>
  </application>
</manifest>
TestActivity.java
public class TestActivity extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
 Toast.makeText(context, "Applications started..!", Toast.LENGTH_LONG).show();
 Log.d("TestActivity", "onCreate!");
  }
 }
MyBootReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBootReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Boot Completed..!", Toast.LENGTH_LONG).show();
 Log.d("MyBootReceiver", "Boot Completed!");
  }
}

That's it..!

Now we will see how to debug BOOT_COMPLETED BroadcastReceiver's onReceive method. To debug normally we simply set the breakpoints and restart the phone/Emulator then we were waiting. But the eclipse won't stop at breakpoints. Because the phone/Emulator restarts and it gets disconnected from the eclipse debugger/LogCat while its booting up.So here is the solution for that.

Steps:
1. Set the breakpoints on MyBootReceiver's 'onReceive' method
2. Open command promt (Run -> cmd)
3. Navigating to SDK's platform-tools
       In my case, it is located in "D:\SW\Android Softwares\eclipse-java-juno-SR1-win32-x86_64\sdk\platform-tools"
      See image references.
4. Type "adb shell"
5. In the shell type "am broadcast -a android.intent.action.BOOT_COMPLETED" or whatever action you want to fire.

Now eclipse stop at breakpoints on MyBootReceiver's 'onReceive' method. That's it.
Well, it’s end for today post, and happy learning!

Cheers.

bCliks

To make something special you just have to believe it’s special!