MOBILE

Building Android Apps : Controlling the Phone with JavaScript (part 3) - Accelerometer

1/16/2011 3:31:58 PM

3. Accelerometer

Next, let’s set up Kilo to duplicate the last entry in the list by shaking the phone. Add the following function to the end of kilo.js:

function dupeEntryById(entryId) {
if (entryId == undefined) {
alert('You have to have at least one entry in the list to shake a dupe.');
} else {
db.transaction(
function(transaction) {
transaction.executeSql(
'INSERT INTO entries (date, food, calories, latitude, longitude) ' +
'SELECT date, food, calories, latitude, longitude ' +
'FROM entries WHERE id = ?;',
[entryId],
function() {
refreshEntries();
},
errorHandler
);
}
);
}
}


Now we need to tell the application when to start and stop watching the accelerometer. We’ll set it up to start watching when the Date panel finishes sliding into view and to stop listening when the panel starts sliding out. To do this, we just need to add the following lines to the document ready function in kilo.js:

$('#date').bind('pageAnimationEnd', function(e, info){
if (info.direction == 'in') {
startWatchingShake();
}
});
$('#date').bind('pageAnimationStart', function(e, info){
if (info.direction == 'out') {
stopWatchingShake();
}
});


Note:

Technically, we can bind to just one of the page animation events, like so:

$('#date').bind('pageAnimationEnd', function(e, info){
if (info.direction == 'in') {
startWatchingShake();
} else {
stopWatchingShake();
}
});

The reason I didn’t do this is that stopWatchingShake() will not be called until after the page animation is complete. Therefore, the accelerometer will be actively watched during the page transition, which can sometimes result in choppy animation.


All that’s left for us to do is write the startWatchingShake() and stopWatchingShake() functions. Add the following functions to the end of kilo.js:

function startWatchingShake() {
var success = function(coords){
var max = 2;
if (Math.abs(coords.x) > max
|| Math.abs(coords.y) > max
|| Math.abs(coords.z) > max) {
var entryId = $('#date ul li:last').data('entryId');
dupeEntryById(entryId);
}
};
var error = function(){};
var options = {};
options.frequency = 100;
sessionStorage.watchId =
navigator.accelerometer.watchAcceleration(success, error, options);
}
function stopWatchingShake() {
navigator.accelerometer.clearWatch(sessionStorage.watchId);
}

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