Android Topics Threads and the MessageQueue

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2010 Android UI, and Networking Mobile Computing Based on android-sdk_2.2 Unless otherwise stated, images are from.
Advertisements

Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
Chapter 6 Jam! Implementing Audio in Android Apps.
Chapter 6: Jam! Implementing Audio in Android Apps.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Multithreading The objectives of this chapter are:
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Android UI, and Networking. Can do most networking on Android Bluetooth only on 2.0, Not supported with version 1.6.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Chapter 2: Simplify! The Android User Interface
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
DUE Hello World on the Android Platform.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Android Boot Camp for Developers Using Java, 3E
Field Trip #32 Digital Alarm Clock By Keith Lynn.
Working in the Background Radan Ganchev Astea Solutions.
Vidispine Data Model Vidispine Bootcamp. Overview Collection Storage File Item Shape Item Component Shape Component Metadata abstract entity physical.
Concurrent Programming and Threads Threads Blocking a User Interface.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Module 8 Enhancing User Interface Responsiveness.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
Multithreading The objectives of this chapter are:
Structured Programming The Basics
Chapter 2: Simplify! The Android User Interface
Concurrency in Android
Android Application 2D Graphics cs.
Small talk with the UI thread
Asynchronous Task (AsyncTask) in Android
Android Boot Camp for Developers Using Java, 3E
Introduction to Triggers
Notifications and Services
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Lecture 6: Process, Thread, Task
Threads, Concurrency, and Parallelism
Lecture 28 Concurrent, Responsive GUIs
CS499 – Mobile Application Development
Mobile Computing With Android ACST 4550 Android Animation
CS323 Android Model-View-Controller Architecture
CS323 Android Getting Started
Android Programming Lecture 8
Android App Developing with communication included
CS371m - Mobile Computing Responsiveness.
Android Topics UI Thread and Limited processing resources
CS323 Android Topics Network Basics for an Android App
Android Topics Asynchronous Callsbacks
Add to the Coffee Ordering App
Introduction to AppInventor
Constructors, GUI’s(Using Swing) and ActionListner
Class 4: Repetition Pretest Posttest Counting Flowchart these!
Threads in Java James Brucker.
Using threads for long running tasks.
Lecture 6: Process, Thread, Task
Mobile Computing Dr. Mohsin Ali Memon.
Cosc 5/4730 EmojiCompat library..
Multithreading The objectives of this chapter are:
Android Threads Dimitar Ivanov Mobile Applications with Android
Mobile Computing With Android ACST 4550 Android Animation
Thread per client and Java NIO
UI Elements 2.
Presentation transcript:

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

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?

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

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

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

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.

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

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.

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();

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.

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

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

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

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.

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.

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.