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 ?
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 { ... } }
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:
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()
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With