Android BroadcastReceiver: Unable to instantiate receiver - no empty constructor

Luca S. picture Luca S. · Feb 7, 2014 · Viewed 10.8k times · Source

I have a problem with a BroadcastReceiver. If I declare the action in the manifest in this way:

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

where in the strings.xml I have:

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

everything works well. 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 as the receiver is called:

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

I would keep the working version but the Play store doesn't allow me to publish the app because it expects a string value and not a variable @string/..

my receiver is an outerclass and is 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();

}

}

Answer

130nk3r5 picture 130nk3r5 · Jan 21, 2015

Make the class a static class, otherwise it's "seen" as part of the original containing class instance.

thus:

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

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