21.07.2015 Views

Linux Journal | January 2013 | Issue 225 - ACM Digital Library

Linux Journal | January 2013 | Issue 225 - ACM Digital Library

Linux Journal | January 2013 | Issue 225 - ACM Digital Library

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

INDEPTHdevice’s GPS, accelerometer, compass,microphone and speaker. The APIprovides persistent data storage byallowing access to the device’s contactdatabase, its filesystem and a nativeSQLite database.Let’s look at some code.For the sake of illustration, Ideveloped a simple application. Theapplication is designed to demonstratethree main features: access to thedevice’s GPS sensor, access to theuser’s contacts and the ability to makeAjax calls to remote Web services.The HTML needed to create thisapplication is pretty straightforward.See Listing 1.The content of the sectionis mostly boilerplate. Note that youimport the cordova.js and then yourmain.js JavaScript files, and that theorder is important. In the ,you find a graphic that you bringin from a remote server. Then yousee input fields for your currentGPS coordinates. Next, you havesome form fields that will containinformation from the phone’s contactdirectory, followed by Previous andNext buttons that allow users to scrollthrough their contacts. Finally, thereare two s that will allow theprogram to display witty commentsfrom a remote Web site and anyerror messages that might need to bedisplayed. Figure 1 shows what thepage looks like in a browser.Listing 2 shows the JavaScript codethat makes it all work.Line 1 is a simple boolean flagthat determines whether the scriptis running on a mobile device or aFigure 1. Sample Application Running ina Browser104 / JANUARY <strong>2013</strong> / WWW.LINUXJOURNAL.COM

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!