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() {1
    var currentDate = sessionStorage.currentDate;
    var dailyBudget = localStorage.budget;
    db.transaction(2
        function(transaction) {
          transaction.executeSql(3
            'SELECT SUM(calories) AS currentTotal FROM entries WHERE date = ?;',4 
            [currentDate], 
            function (transaction, result) { var currentTotal = ...

Get Building Android Apps with HTML, CSS, and JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.