[Android] Multiple Background Threads

805
-1

Published on

1. Overview
2. Using Handlers
2.1 Do not block the UI thread
3. Using AsyncTask

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
805
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

[Android] Multiple Background Threads

  1. 1. Android Programming Lesson 9Multiple Background Threads NGUYEN The Linh
  2. 2. Android ProgrammingContents 1 Overview 2 Using Handlers 3 Using AsyncTask 2
  3. 3. Android ProgrammingMultiple Background Threads Overview 3
  4. 4. Android ProgrammingOverview When an application is launched, the system creates a thread of execution for the application, called "main." The main thread is also sometimes called the UI thread. The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. 4
  5. 5. Android ProgrammingOverview If everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. 5
  6. 6. Android ProgrammingOverview Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. 6
  7. 7. Android ProgrammingOverview The user might then decide to quit your application and uninstall it if they are unhappy. 7
  8. 8. Android ProgrammingOverview There are simply two rules to Androids single thread model:  Do not block the UI thread  Do not access the Android UI toolkit from outside the UI thread 8
  9. 9. Android ProgrammingMultiple Background Threads Using Handlers 9
  10. 10. Android ProgrammingUsing Handlers Do not block the UI thread  Because of the single thread model described above, its vital to the responsiveness of your applications UI that you do not block the UI thread. If you have operations to perform that are not instantaneous, you should make sure to do them in separate threads ("background" or "worker" threads). 10
  11. 11. Android ProgrammingUsing Handlers Do not block the UI thread 11
  12. 12. Android ProgrammingUsing Handlers Do not block the UI thread  At first, this seems to work fine, because it creates a new thread to handle the network operation. However, it violates the second rule of the single-threaded model: do not access the Android UI toolkit from outside the UI thread—this sample modifies the ImageView from the worker thread instead of the UI thread.  This can result in undefined and unexpected behavior, which can be difficult and time-consuming to track down. 12
  13. 13. Android ProgrammingUsing Handlers Do not block the UI thread  To fix this problem, Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help: • Activity.runOnUiThread(Runnable) • View.post(Runnable) • View.postDelayed(Runnable, long) 13
  14. 14. Android ProgrammingUsing Handlers Do not block the UI thread 14
  15. 15. Android ProgrammingUsing Handlers Do not block the UI thread  To handle more complex interactions with a worker thread, you might consider using a Handler in your worker thread, to process messages delivered from the UI thread. 15
  16. 16. Android ProgrammingUsing HandlersWorker Thread UI Thread Handler 16
  17. 17. Android ProgrammingUsing HandlersWorker Thread Handler 17
  18. 18. Android ProgrammingUsing Handlers Example 9.1  Using Handlers 18
  19. 19. Android ProgrammingMultiple Background Threads Using AsyncTask 19
  20. 20. Android ProgrammingUsing AsyncTask AsyncTask is an abstract class that provides several methods managing the interaction between the UI thread and the background thread. It’s implementation is by creating a sub class that extends AsyncTask and implementing the different protected methods it provides. 20
  21. 21. Android ProgrammingUsing AsyncTask Step 1 is creating the AsyncTask sub class:  class ProgressTask extends AsyncTask<Params, Progress, Result>{ }  Params: parameter info passed to be used by the AsyncTask.  Progress: the type of progress that the task accomplishes.  The result returned after the AsyncTask finishes. 21
  22. 22. Android ProgrammingUsing AsyncTask Step 1 is creating the AsyncTask sub class:  class ProgressTask extends AsyncTask<Integer, Integer, Void>{ }  The parameter and the progress are of type Integer and the result is Void as our tasks does not return anything (return null). 22
  23. 23. Android ProgrammingUsing AsyncTask Step 2 is overriding the protected methods defined by the AsyncTask class that handle the execution life cycle of the AsyncTask.  We have five methods to implement which are: • onPreExecute: the first method called in the AsyncTask, called on the UI thread. • doInBackground: the method that executes the time consuming tasks and publish the task progress, executed in background thread. 23
  24. 24. Android ProgrammingUsing AsyncTask Step 2  We have five methods to implement which are: • onProgressUpdate: method that updates the progress of the AsyncTask, run on the UI thread. • onPostExecute: the final method that gets called after doInBackground finishes, here we can update the UI with the results of the AsyncTask. • onCancelled: gets called if the AsyncTask.cancel() methods is called, terminating the execution of the AsyncTask. 24
  25. 25. Android ProgrammingUsing AsyncTask Step 3  Execute • ProgressTask task=new ProgressTask(); • task.execute(10);  Cancel • task.cancel(true); 25
  26. 26. Android ProgrammingUsing AsyncTask Example 9.2  Using AsyncTask 26
  27. 27. Android Programming

×