Friday, 16 January 2015

Receiver Life Cycle in Android ?

* Broadcast receivers us a call-and-be-gone model.

* The process hosting the broadcast receiver will be around only for the lifetime of the receiver and no longer.

* Also, the broadcast receiver runs only for the main thread, and it has a hard ten-second timeframe to finish its work.

* You have to follow a pretty roundabout protocol to acomplish more complicated and time-consuming work in a broadcast receiver.

* If you have a broadcast receiver that takes longer than ten seconds, you will need to follow a protocol such as the following :

1- Get hold of a wakelock in the receiver code ( no later ) so that the device is at least partially awake.

2- Issue a startService() call so that the process is tagged as sticky and restartable, if needed, and hangs around.Note that you cannot do the work in the service directly, because it would take more than ten seconds and that would hold up the main thread.This is because the service also runs on the main thread.

3- Start a worker thread from the service.

4- Have the worker thread post a message through a handler to the service or issue a stopService() call on the service.

No comments:

Post a Comment