Android Programming Tutorials

(Romina) #1

TUTORIAL 38


Sensing a Disturbance


Continuing our additions to LunchList, we really need a solution to the age-


old problem of deciding where to go for lunch. For that, a good old-


fashioned random selection would be a fine starting point. But, to make it


more entertaining, we should trigger the selection by shaking the device.


Shake the phone when on the list of restaurants, and a randomly-selected


restaurant will be shown via its DetailForm.


Step-By-Step Instructions


First, you need to have completed the previous tutorial. If you are


beginning the tutorials here, or if you wish to not use your existing work,


you can download a ZIP file with all of the tutorial results, and you can copy


the 37-Camera edition of LunchList to use as a starting point.


Step #1: Implement a Shaker


We need something that hooks into the SensorManager and watches for


shaking events, where a "shake" is defined as a certain percentage over


Earth's gravity, as determined by calculating the total force via the square


root of the sum of the squares of all three dimensional forces.


And if that was total gibberish to you, this is why humankind has developed


encapsulation and copy-and-paste.


357
Free download pdf