Download presentation
Presentation is loading. Please wait.
Published byRudolf Peters Modified over 9 years ago
1
Android Threads
2
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!
3
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
4
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
5
In the DrawableView class, add line 43 18 // this version of onDraw randomly chooses a color 19 // and position for drawing the rectangles 20 protected void onDraw(Canvas canvas) { 21 22 // this is the “paintbrush” 23 Paint paint = new Paint(); 24 25 // 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); 33 34 // set the position randomly 35 int x = (int)(Math.random()*200); 36 int y = (int)(Math.random()*300); 37 38 // draw Rectangle 39 canvas.drawRect(x, y, x+50, y+50, paint); 40 } 41 42 // launch a new background thread 43 if (count > 0) new BackgroundDrawingTask().execute(); 44 45 } 46
6
Create an inner class in the DrawableView class... 47 class BackgroundDrawingTask extends AsyncTask<Integer, Void, Integer> { 48 49 // 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 } 54 55 // this method gets run in the UI thread 56 protected void onPostExecute(Integer result) { 57 // cause the DrawableView to get redrawn 58 invalidate(); 59 } 60 61 } // end of BackgroundDrawingTask class 62 63 } // end of DrawableView class
7
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.