I haven’t been messing around with the Android SDK for too long but I’ve already found some things that I think are worth sharing. Android has a concept of Services, or a process that can sit in the background and run a task without needing to interact with the user. There’s plenty of reasons why a Service might not need to be running all the time (say an alarm clock app with no alarms scheduled), but for the most part, Services need to be started at boot. Here’s how, tested from Android 1.5 to 2.2, since no other example I could find on the Internet was complete for this ever-changing SDK.

This example Service, StartAtBootService, is a member of the package com.example.ssab (StartServiceAtBoot): Be careful with your Service implementation, as the onStart() method is depreciated in newer versions of the SDK!

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class StartAtBootService extends Service 
{
        public IBinder onBind(Intent intent)
        {
            return null;
        }
        
        @Override
        public void onCreate() 
        {
            Log.v("StartServiceAtBoot", "StartAtBootService Created");
        }
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) 
        {
            Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");          
            
            // We want this service to continue running until it is explicitly
            // stopped, so return sticky.
            return START_STICKY;
        }
        
        /*
         * In Android 2.0 and later, onStart() is depreciated.  Use
         * onStartCommand() instead, or compile against API Level 5 and
         * use both.
         * http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
            @Override
            public void onStart(Intent intent, int startId)
            {
                Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");         
            }
         */

        @Override
        public void onDestroy() 
        {
            Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
        }
}

Use any Service you already have instead of the above. Within application in your AndroidManifest.xml define your Service with an intent-filter:

<service android:name="StartAtBootService">
    <intent-filter>
        <action android:name="com.example.ssab.StartAtBootService">
        </action>
    </intent-filter>
</service>

Now the new part. The OS broadcasts ACTION_BOOT_COMPLETED when it has finished booting. Your app can ask to receive this notification by requesting permission in your manifest:

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

Your app now gets the broadcast, but still needs to do something with it. This is done by subclassing the BroadcastReceiver class. As far as I know, ACTION_BOOT_COMPLETED is the only Intent broadcast to apps, but because this could change at any point (and I can all but guarantee it will), do yourself a favor and check the Intent in your onReceive() method:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartAtBootServiceReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("com.example.ssab.StartAtBootService");
            context.startService(i);
        }
    }
}

The last thing you need to do is register your BroadcastReceiver in your manifest, within application:

<receiver android:name="StartAtBootServiceReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
        </action>
        <category android:name="android.intent.category.HOME">
        </category>
    </intent-filter>
</receiver>

That should do it. If you’re using Eclipse, run your app once and then exit the emulator. Then issue emulator -avd your_avd_name to launch your emulator without uninstalling your app. An adb logcat | grep StartAtBootService should show your app starting at boot.