Make: Arduino Bots and Gadgets by Tero Karvinen & Kimmo Karvinen
Author:Tero Karvinen & Kimmo Karvinen [Tero Karvinen]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / General
ISBN: 9781449389727
Publisher: Make
Published: 2011-03-16T16:00:00+00:00
Figure 6-23. Chess Boxing program printing "Bling!" in 10-second intervals
This program has many similarities with the previous example. The only new feature in the code is the timer. You will define a new class variable of the type Handler for the entire ChessBoxing object. In the beginning of the program, within the onCreate() method, you will store a new Handler object and ask the timer to call the update() function, which is essentially your main loop.
Anything that appears in this part of the class declaration will be visible to all methods within the same class. For example, here the Handler variable is usable within the onCreate() method as well as in the update() method.
We've specified the visibility of the variables as private. Within Java, any objects, methods, and variables marked as public are meant for use by other classes. If you are not building an API for other classes, you might as well set the visibility to private.
The Handler class will define the timers, and we will create a Handler type of a variable called handler.
The previous program wrote to a text view, tv, from only one method, onCreate(). Now, the update() method must also access the text view, so the program must declare tv in the beginning of the class.
In the beginning of the program, call the default activity's onCreate() method.
Create a new Handler object as the variable handler. A Handler class constructor does not take any parameters.
When you're setting up a new timer, it is useful to remove old timers to ensure that they do not accrue on top of each other.
We have to tell the removeCallBacks() method which timers are removed. Since this program only times update objects, the program gives update as its parameter.
Set the first timer, asking the timer handler to call our function after 50 milliseconds. Time is defined in milliseconds (ms), one thousandth of a second.
Android's Handler class is designed in such a manner that the function it invokes is called update.run(). We will define the update variable itself as a Runnable class object, which means we'll have to define the run() method.
Since a millisecond is one thousandth of a second, 50ms equals 0.050s, or one-twentieth of a second (50ms / 1,000ms = 5/100 = 1/20). A human being can't really perceive a time period this short.
Create an update object in a special way that allows us to specify the class definition and instantiate it all at once. (Later, you will see a more traditional example of defining classes.) Here, we define a new object of type Runnable that's visible to the rest of the class.
The content of our own run() method runs the task that the timer invokes. Here, the program just prints "Bling!" to the text view, but in a game or animation, all the other main loop code would go here.
Finally, call the timer again. Just as we did when creating the first timer, remove all existing timers to prevent them from accumulating.
Finally, request to call the update.run() again after 10 seconds.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8300)
Test-Driven Development with Java by Alan Mellor(6722)
Data Augmentation with Python by Duc Haba(6637)
Principles of Data Fabric by Sonia Mezzetta(6388)
Learn Blender Simulations the Right Way by Stephen Pearson(6284)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(6158)
Hadoop in Practice by Alex Holmes(5958)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5807)
RPA Solution Architect's Handbook by Sachin Sahgal(5553)
Big Data Analysis with Python by Ivan Marin(5363)
The Infinite Retina by Robert Scoble Irena Cronin(5247)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5147)
Pretrain Vision and Large Language Models in Python by Emily Webber(4328)
Infrastructure as Code for Beginners by Russ McKendrick(4088)
Functional Programming in JavaScript by Mantyla Dan(4038)
The Age of Surveillance Capitalism by Shoshana Zuboff(3950)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3802)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3607)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3578)
