47

I want to create a broadcast receiver as an inner class in my main activity. But I have problems defining the broadcast receiver in manifest xml file, because android can't find it.

Code:

public class MyActivity extends Activity{
    ...

    public class Receiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            ....
        }

    }

    ...
}

Manifest:

<receiver android:name=".org.danizmax.myapp.MyActivity$Receiver" android:enabled="true">
            <intent-filter>
                <action android:name="org.danizmax.myapp.BROADCAST_INITIAL_DATA"></action>
            </intent-filter>
</receiver>

I tried with:

  • .org.danizmax.myapp.MyActivity$Receiver
  • org.danizmax.myapp.MyActivity$Receiver
  • .MyActivity$Receiver
  • .Receiver

I saw others also having similar problems, but did not find any answers.

So is it possible? If not, what's better way to use broadcast receivers?

Thanks!

danizmax
  • 2,446
  • 2
  • 32
  • 41
  • 3
    Try making your inner class Static. Also, what package attribute do you define in your manifest? That determines the starting point of your android:name on the receiver. – Cheryl Simon Dec 09 '10 at 00:01
  • Ah it works now with static class and receiver name defined as .MyActivity$Receiver. Add your comment about making inner class Static to your first answer so I can send you some reputation. Thanks! – danizmax Dec 09 '10 at 09:33
  • Sorry, I didn't see your comment.. you only get notified of comments on questions if it is your question, or if you use an @username at the beginning of the comment. – Cheryl Simon Dec 15 '10 at 18:27
  • it is work but it is not starting after reboot , how can I do that – CompEng Oct 27 '15 at 18:33

3 Answers3

77

Yes, it is possible.

The receiver android:name attribute should look like .path.to.class.MyClass$MyInnerClass

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
12

If you want to this with a non-static inner class, you can't do it via the AndroidManifest.xml. You can however dynamically register the BroadcastReceiver: Receiver as inner class in Android

Community
  • 1
  • 1
Bert Regelink
  • 2,696
  • 23
  • 17
  • 1
    Great reference, but this won't work for media buttons in [Jelly Bean](http://stackoverflow.com/a/13576130). – an00b Feb 26 '13 at 04:12
  • You need to make the class static and public for android to instantiate the broadcast receiver when it is statically declared in the manifest file.as android instantiates it it needs to be a static class otherwise outer class object will also be required to be instantiated. – user2779311 Aug 13 '14 at 17:36
-2

The better way to use BroadcastReceivers would be to make it its own class. That way the code is better organized and you can possible reuse it.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • 2
    Not if you need to call [a method in your main activity](http://stackoverflow.com/questions/15058743/how-do-i-register-in-manifest-an-inner-media-button-broadcastreciver#comment21174031_15058783). And not in [Jelly Bean](http://stackoverflow.com/a/13576130/636571)... – an00b Feb 26 '13 at 04:10