Utsav Gupta
Utsav Gupta

Reputation: 3971

Lifecycle of a broadcast receiver

I want to have a BroadcastReceiver that has to be active forever (100%). Now if I have this registered in the Android manifest will that be the case, or it will get killed when the app itself is destroyed by the Android framework?

Now, I also have a service running in foreground all the time. Will that make sure that my app is never destroyed? And in turn will that make sure that my receiver which is registered in the Android manifest will remain active forever.?

Upvotes: 3

Views: 3934

Answers (1)

fiipi
fiipi

Reputation: 663

a BroadcastReceiver (docs) which is on the manifest is always active and it works even if the activity is no longer running (think about receivers for the intent action android.intent.action.BOOT_COMPLETED which triggers every time the phone is switched on). Receivers which are defined and registered dinamically only work when the app is running (such as LocalBroadcastManager's registerReceiver(...) approach).

See receiver element documentation which states:

Declares a broadcast receiver (a BroadcastReceiver subclass) as one of the application's components. Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running.

Documentation about services says:

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory

So it is likely that the system won't kill the running app to which the service refers.

Upvotes: 6

Related Questions