Reputation: 14678
I am wondering, what would be the difference between registering broadcast receiver statically in the manifest and starting a service? Both will run even if the app is in the background and it seems to me that receiver is much less complicated to code. Am I missing something here?
Upvotes: 1
Views: 1986
Reputation: 30611
A BroadcastReceiver
is an app component which is used to react to a system-wide, inter-app broadcast, such as BOOT_COMPLETED
or WIFI_STATE_CHANGED
. A BroadcastReceiver
should not be used to run AsyncTask
s or perform other such operations; in such cases, it starts a Service
. Broadcasts are used to wake up an app when some other event occurs on the phone. It is analogous to an interrupt in a microprocessor or a SIGNAL
in Linux.
A Service
is an app component that performs some long-running operation, such as a calculation-intensive computation or a web-service call, without requiring a UI component (or without requiring user interaction). A Service
continues working even when the foreground Activity
has been dismissed, and ends of its own accord after completing its work.
A Service
polls, while a BroadcastReceiver
interrupts. The two are different in nature and purpose, and as such are not interchangeable.
Upvotes: 6