MOBILE

Building Android Apps : Controlling the Phone with JavaScript (part 1) - Beep, Vibrate, and Alert

1/16/2011 3:30:04 PM
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 example, is to add a bit of JavaScript to your code:
navigator.notification.vibrate();

Pretty simple, right?

1. 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 located in the ~/Desktop/KiloGap/assets/www/ directory:

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 = result.rows.item(0).currentTotal;
if (currentTotal > dailyBudget) {
var overage = currentTotal - dailyBudget;
var message = 'You are '+overage+' calories over your'
+ 'daily budget. Better start jogging!';
try {
navigator.notification.beep(1);
navigator.notification.vibrate();
} catch(e){
// No equivalent in web app
}
try {
navigator.notification.alert(message,
'Over Budget', 'Dang!');
} catch(e) {
alert(message);
}
}
},
errorHandler
);
}
);
}

Here’s the blow-by-blow description:

Let’s examine the four parameters of the executeSql() method:

Here’s what’s going on in the anonymous function that was passed in as the third parameter:

With our checkBudget() function complete, we can now call it by adding a single line to the success callback of our createEntry() function:

function createEntry() {
var date = sessionStorage.currentDate;
var calories = $('#calories').val();
var food = $('#food').val();
db.transaction(
function(transaction) {
transaction.executeSql(
'INSERT INTO entries (date, calories, food) VALUES (?, ?, ?);',
[date, calories, food],
function(){
refreshEntries();
checkBudget();
jQT.goBack();
},
errorHandler
);
}
);
return false;
}


After you’ve made these changes, save the kilo.js file, open up a command line  and run the following commands to recompile and install it on your phone (change -d to -e if you’d like to use the emulator instead):

ant debug
adb -d install -r ~/Desktop/KiloGap/bin/Kilo-debug.apk

Figure 1. The PhoneGap alert allows you to specify the title and button label


Figure 2. A native JavaScript alert does not allow you to specify the title and button label

Other  
  •  Building Your First Windows Phone 7 Application (part 5) - Styling Your Application
  •  Building Your First Windows Phone 7 Application (part 4) - Customizing Your First Windows Phone Application
  •  Building Your First Windows Phone 7 Application (part 3) - Writing Your First Windows Phone Code
  •  Building Your First Windows Phone 7 Application (part 2) - Using Your First Windows Phone Silverlight Controls
  •  Building Your First Windows Phone 7 Application (part 1) - Creating a Windows Phone Project
  •  Introducing Windows Phone 7 and the Windows Phone Platform
  •  Windows Phone Application Platform
  •  iPhone Application Development : Basic User Input and Output
  •  Mobile Phone Game Programming : A Quick J2ME Primer
  •  Mobile Phone Game Programming : Java As a Mobile Game Platform
  •  Mobile Phone Game Programming : Getting to Know Mobile Platforms
  •  Mobile Application Security : The Apple iPhone - Local Data Storage: Files, Permissions, and Encryption
  •  Mobile Application Security : The Apple iPhone - Permissions and User Controls
  •  iPhone Application Developmen : Using the View-Based Application Template (part 3)
  •  iPhone Application Developmen : Using the View-Based Application Template (part 2) - Preparing the View Controller Outlets and Actions
  •  iPhone Application Developmen : Using the View-Based Application Template (part 1)
  •  Mobile Application Security: Application Format
  •  Mobile Application Security: Security Testing
  •  Mobile Application Security: The Apple iPhone - Development
  •  Building Android Apps : Installing KiloGap in the Emulator
  •  
    Top 10
    Next – Gen Broadband – Optimizing Your Current Broadband Connection (Part 4)
    Next – Gen Broadband – Optimizing Your Current Broadband Connection (Part 3)
    Next–Gen Broadband – Optimizing Your Current Broadband Connection (Part 2)
    Next–Gen Broadband – Optimizing Your Current Broadband Connection (Part 1)
    Side Channel Attacks Explained
    Canon EOS M With Wonderful Touchscreen Interface (Part 3)
    Canon EOS M With Wonderful Touchscreen Interface (Part 2)
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Canon Powershot G15 With Immaculate Photos And Superb Controls
    Fujifilm XF1 - Compact Camera With Retro Design
    Most View
    We Help You Find Your Ideal Smartphone (Part 4)
    XtremeMac Portable Speaker Reviews
    Windows Server 2003 : Subdomains and Delegation
    Samsung Galaxy SIII : Live up to the hype (Part 2)
    The Best Apps and Gear of 2012 (Part 3)
    ASP.NET 4 : Getting More Advanced with the Entity Framework (part 2) - Updates, Inserts, and Deletes
    Windows Server 2008 : Manipulating Services with wmic
    Choosing A... NAS Device (Part 2)
    What to Back Up on Exchange Servers 2010
    Memory update to boost mobile devices : Manufacturer backing
    .NET Security : Programming the Event Log Service (part 2) - Reading Event Logs, Writing Events
    iPhone Programming : Table-View-Based Applications - Building a Model
    SQL Server 2008 : Transact-SQL Programming - Common Table Expressions
    Wireless Threats
    Fake Depth Of Field With Masks
    Managing SharePoint Data : Writing a List Definition and List Instance
    Windows Mobile Security - Permissions and User Controls
    Programming the iPhone : Network Connectivity
    Genuine Accessories For Galaxy SIII
    Instagram Substitutes On Android (Part 1) - Streamzoo, Flickr & PicPlz