Android Shake Detection Library

I recently wanted an Android shake detection library and came across lots of examples like this which are great for explaining the basics of how the sensor system works. Unfortunately, what they detect is simply a fast acceleration. Simply passing the phone to someone else quickly could trigger a “shake”. So let’s develop a better one.

The idea is to store acceleration data for the last couple of seconds and examine that. To trigger a shake event we look for the acceleration changing direction on one axis a couple of times. This isn’t a perfect system, as someone moving the phone in one direction only in stops and starts could produce an acceleration and deceleration that would trigger the algorithm.

But with this problem it’s about striking a balance between making it easy to shake and trigger but not so sensitive that just passing the phone to someone else or putting it down triggers it.

After we detect a shake, we ignore any events for a second. We don’t want two shakes to close together.

One thing to note is that when the screen shuts off due to user inactivity, your application is still in the foreground and is still detecting shakes which may make for a slightly odd user experience. To get round this, when the activity is active I’ve just locked the screen on. See the onResume() and onPause() methods on this class.

The final class is here and the listener class is here. To use it, simply have your activity create this class then pass a listener. Make sure you call onPause() and onResume(). Below is the minimum code needed.

import uk.co.jarofgreen.lib.ShakeDetectActivity;
import uk.co.jarofgreen.lib.ShakeDetectActivityListener;

public class MainActivity extends Activity {

    ShakeDetectActivity shakeDetectActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        shakeDetectActivity = new ShakeDetectActivity(this);
        shakeDetectActivity.addListener(
        new ShakeDetectActivityListener() {
            @Override
            public void shakeDetected() {
                MainActivity.this.triggerShakeDetected();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        shakeDetectActivity.onResume();
    }

    @Override
    protected void onPause() {
        shakeDetectActivity.onPause();
        super.onPause();
    }

    public void triggerShakeDetected() {
        // do something!
    }

}

It’s on github as part of this app and I’ve used it in other apps. Feedback and patches welcome!

EDIT 22nd Feb: I made library easier to use, updated instructions.

14 thoughts on “Android Shake Detection Library”

  1. Hi James ,
    Im considering using your contribution here to integrate as a android plugin with a Unity3D app for mobile.please let me know how i can go about it ?
    please share your thought on this ..
    Thank You
    mukundan

  2. Thanks! Everything is good!
    But in my case, receiving SMS cause my phone to vibrate and shake detected! Is it ok?
    Thanks in advance.

    1. I think you could use it in an Android service but I haven’t tried myself. Might be some complex threading issues to work out! But that might mean terrible battery life.

  3. Thanks for the library! But I want it to detect 3 different direction x,y,x and give different response to the corresponding shake, may i know how can i do that? thanks

Comments are closed.