Search code examples
androidservicebroadcastreceiver

Android BroadcastReceiver and killing process


let's assume we start a service from BrodcastReceiver on some broadcast. Can the process be killed after onReceive() returns and before service is started? And if so, how can this behavior be prevented?


Solution

  • If the BroadcastReceiver has called Context.startService() and it receives back a non-null ComponentName object, then no. That means the Service was found in the system and has been "started". Note that the start is asynchronous, so there's no guarantee that the Service has received its onStartCommand() callback before the receiver exits its onReceive() callback.

    Also note that even though the BroadcastReceiver has started the Service, by default this has no effect on power management. So it is possible for your Service to not get called back until the device is fully up and running - it could go back to sleep after your BroadcastReceiver finishes but before your Service gets to run.