Receivers and Long-Running Tasks

So what do you do if you want a broadcast intent to kick off a longer-running task than the restrictions of the main run loop allow? You have two options.

The first is to put that work into a service instead and start the service in your broadcast receiver’s small window of opportunity. A service can queue up multiple requests and run them in order or otherwise manage requests as it sees fit. This is the method we recommend. A service has a much longer window it can use to perform work, but it may still be stopped if it runs for long periods. (This threshold varies by OS version and device, but it is generally on the order of several minutes on newer devices.) You can also choose to run a service in ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.