Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BroadcastReceiver: Unable to instantiate receiver - no empty constructor

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();

}

}

like image 777
Luca S. Avatar asked Dec 20 '22 18:12

Luca S.


1 Answers

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

like image 51
130nk3r5 Avatar answered Mar 18 '23 10:03

130nk3r5