The document discusses AsyncTask in Android, which is used to offload long-running tasks from the main UI thread. It explains that there is a single UI thread that handles user interaction and drawing to the screen. Long tasks would cause the app to hang if done on this thread. AsyncTask allows running tasks asynchronously on a background thread, then returning results to the UI thread. It describes the four steps AsyncTask goes through - onPreExecute, doInBackground, onProgressUpdate, and onPostExecute. An example task of uploading movie data to cloud storage is provided.
3. Async Task
Anjan Debnath
UI Thread
● In Android Application, there’s always at least one main thread that executes
the most important tasks in an app:
● handling user interaction,
● drawing pixels on the screen, and
● launching activities.
● This thread is called the UI Thread, or Main Thread.
4. Async Task
Anjan Debnath
UI Thread (Cont..)
● All of our application’s components (Activities, Services, ContentProviders,
BroadcastReceivers, etc.) are created within this thread, and any system
calls made to those components are performed there as well.
5. Async Task
Anjan Debnath
Background Thread
● Background threads are designed to handle any potentially long tasks that
may hang your application. Typical examples of such tasks are network
operations, which can involve unpredictable delays.
6. Async Task
Anjan Debnath
Background Thread
Fortunately, the Android SDK provides developers with a few tools for managing
threads, including AsyncTask, which is designed to offload some of the app’s
work from the main thread.
7. Async Task
Anjan Debnath
Asynchronous tasks use:
● Params, parameters that are sent to the task upon execution.
● Progress, the progress units that are published during the background
computation.
● Result, the results of the background computation.
● private class MyTask extends AsyncTask<Void, Void, Void> { ... }
8. Async Task
Anjan Debnath
When an asynchronous task is executed, the task goes through 4
steps:
● onPreExecute: a step used to set up the task. Invoked on the UI thread
before the task is executed.
● doInBackground: a step used to perform the actual task
● onProgressUpdate: a step used to update the current progress status of the
task that is being performed in doInBackground
● onPostExecute: once doInBackground finishes executing the task, this step
delivers the result back to the main UI thread and stops the AsyncTask
process.
11. Async Task
Anjan Debnath
Cancelling a task
● A task can be cancelled at any time by invoking cancel(boolean).
● Invoking this method will cause subsequent calls to isCancelled() to return
true.
● To ensure that a task is cancelled as quickly as possible, you should always
check the return value of isCancelled() periodically
fromdoInBackground(Object[]), if possible (inside a loop for instance.)
12. Async Task
Anjan Debnath
Assignment
● Using Async Task you have to upload the data Movie Name and Director
name to cloud.
● To store data to cloud you can you Firebase or you can setup your own
server using Apache & MySQL.