How to invoke Ajax based on phone shake?

2.9k views Asked by At

I am trying to make my first web application for mobile devices. Is it possible to capture the "shake event" on android?

I was looking on jquery moble library but there is no shake event or accelerometer events.

Is it possible to have a Ajax call based on shake event???

Thanks Arman.

4

There are 4 answers

1
Thiago On BEST ANSWER

Man, maybe a plugin could do that, but only with JqueryMobile I think you won't be able to get the shake event.

Here's a list of Jquery events: http://jquerymobile.com/demos/1.0a2/experiments/api-viewer/index.html

0
Andreas Lyngstad On

I am not an expert on this, but I do not think you have access to accelerometer, compass and other "local" events trough the mobile browser. If you want to make an local app with javasrcipt, css and HTML, you can look into phonegap, titanium or rhodes.

EDIT: Rhodes is for ruby. It similar to ruby on rails. So you use ruby, javascript, HTML and css.

Then you'll have access to all the events you want.

Read this for more info Mobile Apps cross-platform development challenge: PhoneGap vs. Titanium vs. Rhodes

0
Nuri Hodges On

(Android)

You could script an "event proxy" that is started onLoad by attaching a Javascript interface to your WebView, here's some pseudocode:

    [...]

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.addJavascriptInterface(new JavaScriptInterface(), "android");
    mWebView.loadUrl("file:///android_asset/www/index.html");

}

final class JavaScriptInterface {
    JavaScriptInterface () { }  
    public void startEventProxy() {
        // capture and pass shake events to javascript
    }   
}

(iPhone)

You can interface with the Device Motion API, available in Safari Mobile.

http://plugins.jquery.com/project/tilt

0
gertiMN On

shake.js provides support for a shake event that should make it easy to implement the desired functionality:

https://github.com/alexgibson/shake.js