38

I have inner class as broadcast receiver:

public class ManualBacklightReceiver extends BroadcastReceiver {

    public static final String ACTION_MANUAL_BACKLIGHT = "com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT";

    public ManualBacklightReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("ManualBacklightReceiver", intent.getAction());
    }

};

AndroidManifest:

<receiver android:name=".statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver">
        <intent-filter>
            <action android:name="com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT"/>
        </intent-filter>            
    </receiver>

And when I send the intent with this code: Intent intent = new Intent();

intent.setAction("com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.sendBroadcast(intent);

I get these exceptions:

java.lang.RuntimeException: Unable to instantiate receiver com.android.systemui.statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver:
java.lang.InstantiationException: can't instantiate class com.android.systemui.statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver; no empty constructor
Caused by: java.lang.InstantiationException: can't instantiate class com.android.systemui.statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver; no empty constructor

But I have an empty constructor! Why it doesn't work?

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
artem
  • 16,382
  • 34
  • 113
  • 189

1 Answers1

121

You need to declare your inner class as static. Otherwise, an inner class is associated with an instance of your outer class.

Check out the Java Nested Classes tutorial for details. Here is a snippet:

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.

and:

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)

jarrad
  • 3,292
  • 1
  • 23
  • 15
  • Thanks, that worked for me. But I can not access the fields of OuterClass instance from nested class? What should I do, if the outer class is the view, and I need to modify it when the intent is received? – artem Apr 24 '12 at 20:26
  • 8
    @RankoR: "But I can not access the fields of OuterClass instance from nested class?" -- correct. "What should I do, if the outer class is the view, and I need to modify it when the intent is received?" -- register your `BroadcastReceiver` via `registerReceiver()` from your `Activity`, rather than via the manifest. – CommonsWare Apr 24 '12 at 21:10
  • I tried to do it this way, but it doesn't work, method onReceive is not called. Receiver was created correctly. I'm doing it in the SystemUI package from Android source. – artem Apr 25 '12 at 09:45