Search code examples
androidserviceandroid-manifestintentfilterandroid-broadcastreceiver

Declaring receivers&services on the manifest


My receiver is declared in this way:

<receiver android:name=".receiverName"
android:enabled="true"/>

And here's my service:

<service android:name=".serviceName"
android:enabled="true"/>

But I was wondering: to make them work must I add an <intent-filter> in each one?


Solution

  • to make them work must I add an <intent-filter> in each one?

    That depends on what you are using them for.

    An <intent-filter> is to allow other apps, or sometimes the operating system, to communicate with your components. That is why you have an <activity> with an <intent-filter> for the MAIN action and the LAUNCHER category — home screen launchers know to look for those and give the user the ability to display those activities.

    So, if your plan is to use the service purely within your own app, you do not need an <intent-filter>. The same thing holds true for your receiver. If, on the other hand, you are expecting other apps to start the service, bind to the service, or send you a broadcast, then you will want an <intent-filter>.