Is it possible to define a broadcast receiver as an inner class in manifest file?

31,879

Solution 1

Yes, it is possible.

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

Solution 2

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

Share:
31,879

Related videos on Youtube

danizmax
Author by

danizmax

Active in Linux world, my current projects are in Java EE (full stack). My hobbies are photography, cycling, running, hiking and gaming together with other people.

Updated on July 09, 2022

Comments

  • danizmax
    danizmax almost 2 years

    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!

    • Cheryl Simon
      Cheryl Simon over 13 years
      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.
    • danizmax
      danizmax over 13 years
      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!
    • Cheryl Simon
      Cheryl Simon over 13 years
      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.
    • Ersin Gulbahar
      Ersin Gulbahar over 8 years
      it is work but it is not starting after reboot , how can I do that
  • danizmax
    danizmax over 13 years
    Inner class must be Static...
  • an00b
    an00b about 11 years
    Not if you need to call a method in your main activity. And not in Jelly Bean...
  • an00b
    an00b about 11 years
    Great reference, but this won't work for media buttons in Jelly Bean.
  • Bear
    Bear over 10 years
    Boom. Many thanks for this :-)
  • user2779311
    user2779311 over 9 years
    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.
  • Radu Simionescu
    Radu Simionescu over 9 years
    @danizmax you are a genius!
  • Victor Häggqvist
    Victor Häggqvist over 9 years
    Also remember android:enabled="true" on the <receiver>
  • Ersin Gulbahar
    Ersin Gulbahar over 8 years
    @danizmax ;Bear ;Victor it is work but it is not starting after reboot , how can I do that
  • Andrew S
    Andrew S over 7 years
    How do you overcome this with a non-static inner class?