在清单上声明接收器和服务

Declaring receivers&services on the manifest

我的接收器是这样声明的:

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

这是我的服务:

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

但我想知道:要使它们工作,我必须在每个中添加一个 <intent-filter> 吗?

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

这取决于您使用它们的目的。

<intent-filter> 是允许其他应用程序(有时是操作系统)与您的组件进行通信。这就是为什么你有一个 <activity> 和一个 <intent-filter> 用于 MAIN 操作和 LAUNCHER 类别 - 主屏幕启动器知道寻找那些并让用户能够显示那些活动。

因此,如果您的计划是纯粹在您自己的应用程序中使用该服务,则不需要 <intent-filter>。同样的事情也适用于你的接收器。另一方面,如果您希望其他应用程序启动该服务、绑定到该服务或向您发送广播,那么您将需要 <intent-filter>.