Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast Receiver as inner class in Android

Tags:

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 ?

like image 599
Kunal P.Bharati Avatar asked Aug 31 '10 12:08

Kunal P.Bharati


2 Answers

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     {         ...     } } 
like image 157
Bert Regelink Avatar answered Oct 14 '22 11:10

Bert Regelink


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")
like image 27
BalusC Avatar answered Oct 14 '22 11:10

BalusC