Search code examples
androidandroid-serviceandroid-broadcast

BroadcastReceiver crashes on boot when trying to start simple Service


I have been at this for a long time now and I don't know why it's not working. I'm trying to start a service when the device starts up, using a BroadcastReceiver. When the device starts up, it crashes and asks me if the activity has been registered in the AndroidManifest.

I'm using the Genymotion emulator, and have tried deleting the app as well as cleaning the project in Eclipse. I have also tried adding the full name to the android:name "com.gordon.status.service.StartBackgroundSyncService," and that is not working either.

BroadcastReceiver:

public class AutostartReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Intent i = new Intent(context, StartBackgroundSyncService.class);       
        context.startService(i); 
    }
}

Manifest:

    <service
        android:name="service.StartBackgroundSyncService"
        android:enabled="true"
        android:exported="false"/>
    <service        
        android:name="service.LinkedInBackgroundService"
        android:enabled="true"/>
    <receiver android:name="receiver.AutostartReceiver"
              android:exported="false"
              android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <!--  For some HTC devices -->
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

The service (it should display Hello when the device starts up)

public class StartBackgroundSyncService extends Service
{
    @Override
    public void onCreate()
    { 
        super.onCreate(); 

        System.out.println("HELLO=========================================================");
        System.out.println("=============================================================="); 
    }

    public int startCommand(Intent intent, int flags, int startId)
    { 
        return super.onStartCommand(intent, flags, startId); 
    }   

    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }
}

Logcat:

10-17 20:48:07.438: E/AndroidRuntime(1147): java.lang.RuntimeException: Unable to start receiver receiver.AutostartReceiver: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gordon.status/service.StartBackgroundSyncService}; have you declared this activity in your AndroidManifest.xml?
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ActivityThread.access$1500(ActivityThread.java:141)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.os.Looper.loop(Looper.java:137)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at java.lang.reflect.Method.invokeNative(Native Method)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at java.lang.reflect.Method.invoke(Method.java:511)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at dalvik.system.NativeStart.main(Native Method)
10-17 20:48:07.438: E/AndroidRuntime(1147): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gordon.status/service.StartBackgroundSyncService}; have you declared this activity in your AndroidManifest.xml?
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ContextImpl.startActivity(ContextImpl.java:957)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ContextImpl.startActivity(ContextImpl.java:939)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at receiver.AutostartReceiver.onReceive(AutostartReceiver.java:16)
10-17 20:48:07.438: E/AndroidRuntime(1147):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)

Solution

  • Somewhere, not in the code that you have here, you are calling startActivity() on an Intent pointing at your StartBackgroundSyncService. Since StartBackgroundSyncService is not an activity, it is failing. Find where you are calling startActivity() on such an Intent and change it to point to an actual activity in your app.