开发者

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:

&开发者_StackOverflow社区lt;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:

  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")


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

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


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" />


you dont need to use $... If you get warning for a class which is not actually an inner class, it's because you are using uppercase characters in your package name, which is not conventional.

I tried also to change only the first letter of the package name to be lower case, the warning and error disappeared.


I've just met the same problem.

I have a service to communicate with many activities, and I also have a receiver in my service as inner class. when the receiver get message ,it has to call the method of service. so it goes on like this :

1. Service code:

public class XXService extends Service{

     // I want the inner receiver call the methd of XXService,
    // so I have to write this constructor like this.

    private XXService instance;
    public XXService(){instance = this;}
    public XXService getInstance(){
       return instance;
    }
    public void sayHello(){
    }
    public static class XXReceiver extends BroadcastReceiver{

        onReceive(......){
            XXService.getInstance.sayHello();
        } 
   }
}

2. Register receiver in manifest.xml :

<receiver android:name=".XXService$XXReceiver" android:enabled="true"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜