Broadcast Receiver as inner class in Android

In my code there is an inner class that extends BroadcastReceiver.

And I have added the following line to the AndroidManifest.xml:

<receiver android:name="OuterClass$InnerClass android:enabled="true"/>

But I am receiving the following error:

unable to instantiate receiver org.example.test.OuterClass$InnerClass

How can I solve this issue ?


Solution 1:

An (non-static) inner class cannot be instantiated by Android via the AndroidManifest.xml (Android developer documentation on BroadcastReceiver):

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.

So you can dynamically register the receiver. In my application I wanted to do the same for using Google's Cloud to Device Messaging (C2DM) and my original AndroidManifest.xml contained:

<application...>
    <receiver android:name=".MyC2dmReceiver" android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.myapp" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.example.myapp" />
        </intent-filter>
    </receiver>
</application>

I removed that receiver section and dynamically registered the receiver as follows:

public class AndroidService extends IntentService
{
    ... 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.google.android.c2dm.intent.RECEIVE");
        filter.addAction("com.google.android.c2dm.intent.REGISTRATION");
        filter.addCategory("com.example.myapp");
        this.registerReceiver(new MyC2dmReceiver(), filter, "com.google.android.c2dm.permission.SEND", null);
        return super.onStartCommand(intent,flags,startId);
    }

    public class MyC2dmReceiver extends BroadcastReceiver
    {
        ...
    }
}

Solution 2:

The $ notation doesn't denote an inner class, but a static nested class. So there are in theory 2 ways to solve this particular problem:

  1. Denote it as a real inner class, i.e. OuterClass.InnerClass (not sure though if Android will eat that since the instantiation of an inner class is pretty more complex than just doing Class#newInstance().

  2. Declare the class to be a static nested class instead, i.e. add static to class InnerClass {}. This way the OuterClass$InnerClass must be able to create a new instance out of it.

If that doesn't solve the problem, then apparently Android simply doesn't eat it that way. I'd just extract it into its own standalone class then.

See also:

  • Java tutorial - Nested classes
  • Answer with code example how to instantiate an inner or static nested class using reflection (as Android should be doing "under the covers")

Solution 3:

Could it be that there is just a dot and a closing quote missing? Like

<receiver android:name=".OuterClass$InnerClass" android:enabled="true"/>

Solution 4:

This is what worked for me:

public class OuterClass {
    public static class InnerStaticClass extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final Location location = (Location) intent.getExtras().get(LocationClient.KEY_LOCATION_CHANGED);
        }
    }
}

AndroidManifest.xml:

    <receiver android:name=".OuterClass$OuterClassConnector$InnerStaticClass" />