Search code examples
androidbroadcastreceiverbootcompleted

Unable to instantiate receiver java.lang.ClassNotFoundException


I got an error in my android application when it tries to instantiate a receiver that i use to start a service on boot up. The error is obvious, it can not find the class file of my receiver. But everything is ok with my manifest file, the packages and all and i have no clue what is happening. Here is my code:

package dti.obd.reader;

import dti.obd.reader.service.MainService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver 
{
      @Override
      public void onReceive(Context context, Intent intent) 
      {
            Intent serviceIntent = new Intent(MainService.class.getName());
            context.startService(serviceIntent);
      }
}

And my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dti.obd.reader"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <service android:name=".service.MainService" >
            <intent-filter >
                <action android:name="dti.obd.reader.service.MainService" />
            </intent-filter>
        </service>

        <receiver android:name="dti.obd.reader.BootReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>
    </application>

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

</manifest>

Does anyone knows the erro? It seems that the package and the names are all ok...


Solution

  • You have to put your Receiver in some package. The system won't be able to instantiate it if it is on the main package.

    I had the same problem. Fortunately before searching the error on internet I was doing another java project. I just realized that the bug in there was similar to this one. I tried it just now and worked. :)