Receiver as inner class in Android

前端 未结 6 1685
广开言路
广开言路 2020-11-27 06:14

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

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

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:27

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

提交回复
热议问题