Android Threads Dimitar Ivanov Mobile Applications with Android

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
USING ANDROID WITH THE INTERNET. Slide 2 Network Prerequisites The following must be included so as to allow the device to connect to the network The.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading.
Cosc 5/4730 A little on threads and Messages: Handler class.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Nachos Phase 1 Code -Hints and Comments
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Networking Android Club Networking AsyncTask HttpUrlConnection JSON Parser.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Asst Prof. Saeed Ahmadi Software Engineering CSF Kabul University 1.
HTTP and Threads. Getting Data from the Web Believe it or not, Android apps are able to pull data from the web. Developers can download bitmaps and text.
1 Web Based Programming Section 8 James King 12 August 2003.
Working in the Background Radan Ganchev Astea Solutions.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
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.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Java Thread and Memory Model
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Asynchronous Programming Writing Asynchronous Code in Java SoftUni Team Technical Trainers Software University
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
Java Thread Programming
Views in iOS Mobile apps for iPhone & iPad Telerik Software Academy
Concurrency in Android
Small talk with the UI thread
Asynchronous Task (AsyncTask) in Android
Android Multi-Threading
Multi Threading.
CS240: Advanced Programming Concepts
Operating System (013022) Dr. H. Iwidat
Lecture 6: Process, Thread, Task
CompSci 230 Software Construction
CS499 – Mobile Application Development
Mobile Computing With Android ACST 4550 Android Animation
Developing Android Services
Android Programming Lecture 8
CS371m - Mobile Computing Responsiveness.
Multithreaded Programming
Android Topics UI Thread and Limited processing resources
Recitation 7 October 7, 2011.
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
Android Developer Fundamentals V2
Prof. Leonardo Mostarda University of Camerino
Chapter 3: Processes.
Threads, Handlers, and AsyncTasks
Threads in Java James Brucker.
Using threads for long running tasks.
Lecture 6: Process, Thread, Task
Mobile Computing Dr. Mohsin Ali Memon.
Chapter 4 Threads “Most modern applications are multithreaded”
Presentation transcript:

Android Threads Dimitar Ivanov Mobile Applications with Android Telerik Software Academy http://academy.telerik.com

Today’s Topics Threading Overview Android’s UI Thread The AsyncTask Class The Handler Class

What is a Thread ? Conceptual View Implementation View Parallel computation running in a process Implementation View A program counter and a stack With heap and static areas that are shared with other threads.

What is a Thread ?

Java Threads Represented by an object of type Java.Lang.Thread. Implements the runnable interface void run() Some thread methods void start() Starts the Thread void sleep(long time) Sleeps for the given period

Java Threads Void wait() Void notify() Current thread waits until another thread invokes notify() on this object. Void notify() Wakes up a single thread that is waiting on this object.

Basic Thread Use Case Instantiate a Thread object Invoke the Thread’s start() method Thread’s run() method get called Thread terminates when run() returns

Basic Thread Use Case

ThreadingNoThreading Demo Application displays two buttons LoadIcon Load a bitmap from a resource file & display Show loaded bitmap Other Button Display some Toast text

AsyncTask Provides a structured way to manage work involving background & UI Threads Background Thread Perform work Indicate progress UI Thread Does setup Publishes intermediate progress User results

AsyncTask Generic Class Generic Type Parameters Class AsyncTask<Params,Progress,Result> { … } Generic Type Parameters Params – Type used in background work

AsyncTask void onPreExecuted() Result Runs in UI Thread before doInBackground() Result doInBackground(Params…params) Performs work in background Thread May call void publishProgress(Progress… values)

AsyncTask void onProgressUpdate(Progress… values) Invoked in response to publishProgress() void onPostExecute(Result result) Runs after doInBackground()

Async Demo

Handler Each handler is associated with a Thread One thread can hand off work to another Thread by sending Messages & Posting Runnables to a handler associated with other Thread. Runnable Contains an instance of the Runnable interface Sender implements response Message Can contain a message code, an object & integer arguments

Handler Architecture

Runnables & Handlers boolean post(Runnable r) Add Runnable to the MessageQueue Boolean postAtTime(Runnable r,long uptimeMillis) Add Runnable to the MessageQueue.Run at a specific time(based on SysteClock.upTimeMillis()) Boolean postDelayed(Runnable r,long delayMillis) Add Runnable to the message queue.Run after the specific amount of time elapsed.

Messages & Handlers Create Message & set Message content Handler.obtainMessage() Message.obtain() Message parameters include Int arg1,arg2, what Object obj Bundle data Many variants.See documentation

Messages & Handlers sendMessage() sendMessageAtFrontOfQueue() Queue Message now sendMessageAtFrontOfQueue() Insert Message now at front of queue sendMessageAtTime() Queue message at the stated time

Handler Live Demo

Android Threads http://academy.telerik.com

Homework Asynchronously download an image from a random web resource and indicate the progress of the download i.e. http://images.google.com