Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Topics Threads and the MessageQueue

Similar presentations


Presentation on theme: "Android Topics Threads and the MessageQueue"— Presentation transcript:

1 Android Topics Threads and the MessageQueue
Review of Basic Threads MessageQueue, Looper, and Handler

2 Review of Basic Threads
What is the main thread of execution for a given Android application? Does every application have its own UI Thread? The UI Thread controls UI objects. What are these objects? Name two app components that are created and performed on the UI Thread? Why do we need Threads in an application? Is it possible to update UI elements from a background thread?

3 What is the main thread of execution for a given Android application?
Question: What is the main thread of execution for a given Android application? Answer: UI Thread

4 Does every application have its own UI Thread?
Question: Does every application have its own UI Thread? Answer: Yes.

5 The UI Thread controls UI objects. What are these objects?
Question: The UI Thread controls UI objects. What are these objects? Answer: View objects

6 Question: Name two app components that are created and performed on the UI Thread? Answer: Activities Intents NOTE: It is a RULE that system calls to these components are performed on the UI thread.

7 Why do we need Threads in an application?
Question: Why do we need Threads in an application? Answer: Heavy Calculations Long initialization of objects Networking Database Operations

8 Is it possible to update UI elements from a background thread?
Question: Is it possible to update UI elements from a background thread? Answer: No.

9 Structure of Runnable //TASK 1: CREATE A RUNNABLE OBJECT Runnable r = new Runnable() { @Override public void run() { while (Processing ) Log.i("THREAD DEMO", "Working on it"); } }; //TASK 2: CREATE A THREAD TO PROCESS A RUNNABLE OBJECT Thread myBackgroundThread = new Thread(r); //TASK 3: START THE THREAD myBackgroundThread.start();

10 Threads and the MessageQueue
Android maintains a queue called a MessageQueue. This queue is always populated with tasks that will update the UI. These tasks are Runnable objects and will be executed in sequence and can be as simple as rendering a button or setting the text of a TextView.

11 The Message Queue and the Looper
Tasks within the Message Queue are in a constant loop, called a Looper. A Looper loops over a MessageQueue and dispatches messages one at a time to be executed. Looper

12 The Message Queue and the Looper
A background thread cannot add a task to the MessageQueue. Looper Background Thread

13 The Message Queue and the Looper
A background Thread needs the assistance of a Handler to add tasks to a MessageQueue. Tasks are not added directly to a MessageQueue, but rather through Handler objects associated with the Looper. Background Thread

14 Tasks on the MessageQueue are Runnable objects.
It is important to remember that adding and removing messages to and from the MessageQueue is done by the Handler.

15 In the image, Handler A adds and removing messages to and from the MessageQueue.
A Looper dispatches messages one at a time to be executed.

16 Create a handler and a looper.
Create an instance of a Handler. Note: The Handler object will post Runnable tasks to the main MessageQueue. The posted Runnable tasks will be simple – set the TextView with the count value. Instantiate the Handler and provide a reference to the MessageQueue for the UI Thread (the main default looper). Create a backbround Thread containing a Runnable Object. Specify the work that is performed by the background thread. This code will be placed in the abstract method run(). Use a Handler to constrict a Runnable object that updates the MessageQueue. Post this Runnable object to the MessageQueue. Start the background Thread.


Download ppt "Android Topics Threads and the MessageQueue"

Similar presentations


Ads by Google