Java – Android Broadcast Receiver: Unable to instantiate receiver – no empty constructor

Android Broadcast Receiver: Unable to instantiate receiver – no empty constructor… here is a solution to the problem.

Android Broadcast Receiver: Unable to instantiate receiver – no empty constructor

I have a problem with BroadcastReceiver. If I declare the operation in the list this way:

    <receiver android:name="com.app.activity.observer.DataEntryObserver" >
        <intent-filter>
            <action android:name= "@string/action_db_updated" />
        </intent-filter>
    </receiver>

My position in strings.xml:

       <string name="action_db_updated">com.app.DB_UPDATED</string>

Everything works fine. But if I change it to:

    <receiver android:name="com.app.activity.observer.DataEntryObserver" >
        <intent-filter>
            <action android:name= "com.app.DB_UPDATED" />
        </intent-filter>
    </receiver>

I have this exception because the receiver is called:

java.lang.RuntimeException: Unable to instantiate receiver com.app.activity.observer.DataEntryObserver: java.lang.InstantiationException: Unable to instantiate class com.app.activity.observer.DataEntryObserver; there is no empty constructor

I’ll keep the working version, but the Play Store won’t allow me to publish the app because it requires a string value instead of a variable @string/..

My receiver is an external class, defined as:

   public class DataEntryObserver extends BroadcastReceiver{

private AppUsageLoader dLoader;

public DataEntryObserver(AppUsageLoader dLoader) {
    this.dLoader = dLoader;

IntentFilter filter = new IntentFilter(
            ReaLifeApplication.ACTION_DB_UPDATED);
    dLoader.getContext().registerReceiver(this, filter);
}

@Override
public void onReceive(Context arg0, Intent arg1) {

 Tell the loader about the change.
    dLoader.onContentChanged();

}

Solution

Make the class

static, otherwise it will be “treated” as part of the original containing class instance.

Therefore:

public static class DataEntryObserver extends BroadcastReceiver{
public DeviceAdminSampleReceiver() {
            super();
        }
...

https://stackoverflow.com/a/10305338/1285325

Related Problems and Solutions