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.

Slides:



Advertisements
Similar presentations
Threads, Surface Views and Real-Time Games. Background Most of the Android apps we’ve covered so far have been single threaded – And Event driven – An.
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Layouts.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Written by: Dr. JJ Shepherd
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
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.
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing.
Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
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.
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
CS12420 – Swing and threads Lynda Thomas
Visual Basic .NET BASICS
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
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.
Review of CIS 120 Concepts: What you said you want….
Android Graphics Library. Color Android colors are represented with four numbers, one each for alpha, red, green, and blue (ARGB). Each component can.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
COP 3330 Notes 3/7. Today’s Topics Exceptions Abstract Classes.
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
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.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Android Application 2D Graphics cs.
Small talk with the UI thread
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
Concurrency in Android
CS240: Advanced Programming Concepts
CSE 486/586 Distributed Systems Android Programming --- 2
Android Layouts 24 July 2018 S.RENUKADEVI/AP/SCD/ANDROID LAYOUTS 1.
Reactive Android Development
Java Finish command line guessing game, then….
Condition Variables and Producer/Consumer
CS371m - Mobile Computing Responsiveness.
Condition Variables and Producer/Consumer
Android Topics UI Thread and Limited processing resources
Mobile Computing With Android ACST 4550 Android Database Storage
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
Android Developer Fundamentals V2
Threads, Handlers, and AsyncTasks
Threads in Java James Brucker.
Using threads for long running tasks.
Random Numbers while loop
Android Threads Dimitar Ivanov Mobile Applications with Android
Mobile Computing With Android ACST 4550 Android Animation
CIS 470 Mobile App Development
Presentation transcript:

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 the “main thread” prohibits UI events from being handled This means that any long-running code should run in a background thread However, background threads are not allowed to modify UI elements!

How Android threading works Create a class that extends AsyncTask To start the new thread, call the AsyncTask's execute method When execute is called, Android does the following: 1.runs onPreExecute in the main (UI) thread 2.runs doInBackground in a background thread 3.runs onPostExecute in the main (UI) thread

Let's add threads to our app! We want the app to “sleep” for two seconds and then redraw the random shapes We will create an inner class called BackgroundDrawingTask that extends AsyncTask and implement two methods: – doInBackground will just sleep for two seconds – onPostExecute will call invalidate the DrawableView, causing the View to be redrawn We then create a BackgroundDrawingTask and call its execute method to start the new thread

In the DrawableView class, add line // this version of onDraw randomly chooses a color 19 // and position for drawing the rectangles 20 protected void onDraw(Canvas canvas) { // this is the “paintbrush” 23 Paint paint = new Paint(); // loop for each rectangle to draw 26 for (int i = 0; i < count; i++) { 27 // set the color randomly 28 int whichColor = (int)(Math.random() * 4); 29 if (whichColor == 0) paint.setColor(Color.RED); 30 else if (whichColor == 1) paint.setColor(Color.GREEN); 31 else if (whichColor == 2) paint.setColor(Color.BLUE); 32 else paint.setColor(Color.YELLOW); // set the position randomly 35 int x = (int)(Math.random()*200); 36 int y = (int)(Math.random()*300); // draw Rectangle 39 canvas.drawRect(x, y, x+50, y+50, paint); 40 } // launch a new background thread 43 if (count > 0) new BackgroundDrawingTask().execute(); } 46

Create an inner class in the DrawableView class class BackgroundDrawingTask extends AsyncTask<Integer, Void, Integer> { // this method gets run in the background 50 protected Integer doInBackground(Integer... args) { 51 try { Thread.sleep(2000); } catch (Exception e) { } 52 return 1; 53 } // this method gets run in the UI thread 56 protected void onPostExecute(Integer result) { 57 // cause the DrawableView to get redrawn 58 invalidate(); 59 } } // end of BackgroundDrawingTask class } // end of DrawableView class

Where can you go from here? Create a game in which the user has to touch on the randomly-appearing shapes The shapes change color when they're touched, and the “score” is tracked As the user advances, the number of shapes and/or the frequency with which they are redrawn can be increased