Learning Android™ Application Programming: A Hands-On Guide to Building Android Applications
by James Talbot, Justin McLean, Jorge Hernandez
Making an Android Device Vibrate
Sometimes a device’s screen may not be visible (for example, if it’s in someone’s pocket), so you need to indicate that time has passed in a nonvisual way. Making the device vibrate is a good way to do this.
Let’s set up the code to vibrate once every 5 minutes, twice every 15 minutes, and three times every hour while the timer is running.
1. Add a property called vibrate of type Vibrator to the TimerActivity class:
protected Vibrator vibrate;
2. Add a property called lastSeconds of type long. This is needed because the run method is called several times a second, and you want the device to vibrate only once.
protected long lastSeconds;
3. In the onStart method, set up the vibrate property by calling getSystemService ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access