Chapter 6. Using Background Tasks

In this chapter, we will cover the following recipes:

  • Asynchronous tasks
  • Starting services
  • Stopping services
  • Handling simultaneous service requests
  • Starting services automatically
  • Communicating with running services
  • Critical tasks

Introduction

Multitasking is what computers do, and they do it amazingly well. In the case of mobile apps, the user expects the app to always be responsive even if a complex task is being executed.

Most devices are fast enough to execute tasks as needed, but sometimes those tasks take longer than a few milliseconds. If that task was to be executed on the main, or UI thread, then the UI would freeze.

Users demand that not only an app be responsive when executing long-running tasks, but also that animations and transitions run smoothly. In such a case, the task has to be moved into the background so that the app continues to give the impression that it is waiting for the user, when in fact, it is working hard.

Some tasks are not work happing but rather managing the tasks being run by others. This can include playing music or downloading a file. The app has already handed the task over to the operating system, and all that the user cares about is starting a new app or task.

The device must remain responsive for the user while continuing the previous task in the background, but it should still listen for the user to jump back into the previous task. In the case of a file download, the user may decide that it is no longer needed and cancel the download. The state should be restored and the task should be canceled without a delay. After all this, the user will probably switch to another app.

The user is not as fast as a computer, but they will notice immediately if there are any delays. As the computer is able to multitask at high speed, there is no real reason as to why an app should freeze for any amount of time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset