Is it possible to define a broadcast receiver as an inner class in manifest file?
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
Related videos on Youtube
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, 2022Comments
-
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 over 13 yearsTry 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 over 13 yearsAh 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 over 13 yearsSorry, 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 over 8 yearsit is work but it is not starting after reboot , how can I do that
-
danizmax over 13 yearsInner class must be Static...
-
an00b about 11 yearsNot if you need to call a method in your main activity. And not in Jelly Bean...
-
an00b about 11 yearsGreat reference, but this won't work for media buttons in Jelly Bean.
-
Bear over 10 yearsBoom. Many thanks for this :-)
-
user2779311 over 9 yearsYou 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 over 9 years@danizmax you are a genius!
-
Victor Häggqvist over 9 yearsAlso remember
android:enabled="true"
on the<receiver>
-
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 over 7 yearsHow do you overcome this with a non-static inner class?