Building Android Apps with HTML, CSS, and JavaScript, 2nd Edition
by Jonathan Stark, Brian Jepson, Brian MacDonald
Controlling the Phone with JavaScript
The stage is now set for us to start enhancing our application with calls to the native device features. Thanks to phonegap.js, all you have to do to make the phone vibrate for 200 milliseconds, for example, is to add a bit of JavaScript to your code:
navigator.notification.vibrate(200);
Pretty simple, right?
Beep, Vibrate, and Alert
PhoneGap makes beep, vibrate, and alert functions so simple that I’m going to lump them together into one example. Specifically, we’ll set up the app to beep, vibrate, and display a custom alert when the user creates an entry that puts him over his daily calorie budget. To do so, add the following function to the end of the kilo.js (remember to edit the copy in Eclipse, not the original copy from the previous chapters):
function checkBudget() {
var currentDate = sessionStorage.currentDate;
var dailyBudget = localStorage.budget;
db.transaction(
function(transaction) {
transaction.executeSql(
'SELECT SUM(calories) AS currentTotal FROM entries WHERE date = ?;',
[currentDate],
function (transaction, result) { var currentTotal = ...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