Detect direction of shake movement in SensorListener

by TsEMaNN » Fri, 14 Jan 2011 21:36:23 GMT

Sponsored Links
 Hello developers,

I have got a problem concerning the SensorListener. At the moment I
can already detect if the phone got shaken. Addtionally I now want to
detect in which direction the phone is shaken. Do you maybe know which
values I have to consider to detect this?

My code at the moment looks like this:

public class ShakeActivity extends Activity implements SensorListener
   // For shake motion detection.
   private SensorManager sensorMgr;
   private long lastUpdate = -1;
   private float x, y, z;
   private float last_x, last_y, last_z;
   private float raw_x, last_raw_x;
   private static final int SHAKE_THRESHOLD = 800;
   private final float shakeThreshold = 1.5f;

   protected void onCreate(Bundle savedInstanceState) {

      // start motion detection
      sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
      boolean accelSupported = sensorMgr.registerListener(this,

      if (!accelSupported) {
         // on accelerometer on this device

   protected void onPause() {
      if (sensorMgr != null) {
         sensorMgr = null;

   public void onAccuracyChanged(int arg0, int arg1) {
      // TODO Auto-generated method stub

   public void onSensorChanged(int sensor, float[] values) {
      Log.d("sensor", "onSensorChanged: " + sensor);
      if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
         long curTime = System.currentTimeMillis();
         // only allow one update every 100ms.
         if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = values[SensorManager.DATA_X];
            y = values[SensorManager.DATA_Y];
            z = values[SensorManager.DATA_Z];
            raw_x = SensorManager.GRAVITY_EARTH;

            float xChange = Math.abs(x - last_x);
            float speed = Math.abs(x + y + z - last_x - last_y -
                  / diffTime * 10000;

            float deltaX = Math.abs(last_x - x);
            float deltaY = Math.abs(last_y - y);
            float deltaZ = Math.abs(last_z - y);
            float delta_raw_x = raw_x - last_raw_x;
            // Log.d("sensor", "diff: " + diffTime + " - speed: " +
            if (speed > SHAKE_THRESHOLD) {
               Log.d("sensor", "shake detected w/ speed: " + speed);
                     "shake detected w/ speed: " + speed + "; accelX:
                           + xChange, Toast.LENGTH_SHORT).show();
               // now left,right,up,down movement has to be detected
               // somehow
            last_x = x;
            last_y = y;
            last_z = z;
            last_raw_x = raw_x;

Best regards,


Re: Detect direction of shake movement in SensorListener

by Hari Edo » Sat, 15 Jan 2011 03:33:47 GMT

 What is your code giving you that's unsatisfactory?

Depending on your definition of "shake", you could probably
tell the difference between "side to side" vs "up and down"
but you will likely get very inconsistent results if you try to
distinguish "leftward" vs "rightward".  Your hand often
anticipates a little, moving left before a big rightward jerk.
(Google "anticipation animation" for more.)


Sponsored Links

Re: Detect direction of shake movement in SensorListener

by bklik » Mon, 21 Feb 2011 03:53:39 GMT

 I think what you're after is the difference in values between X (left
and right), Y (up and down), Z (toward you and away).

My code looks something like this:

public void onSensorChanged(SensorEvent event) {
    long curTime = System.currentTimeMillis();

    x = Math.abs(event.values[0]);
    y = Math.abs(event.values[1]);
    z = Math.abs(event.values[2]);

    if((x+y+z) > ACCEL_THRESHOLD){
        shakeLevel += (x+y+z);
        if(shakeCount >= 3){
            shakeCount = 0;
            shakeLevel = 0;

        if(shakeLevel > SHAKE_THRESHOLD){
            // Do something because the phone has been shaken.

What I'm doing is looking for repetitive amounts of movement in any
direction, over a certain threshold, over time. You could break this
down into counts for X, Y, and Z rather then adding them all together
like I am. Then, just play with different thresholds that trigger when
you expect.



Other Threads

1. How do I transfer my .adk to the \system\app directory?

I built a simple hello world application and I have the .adk file.  i
 transfered the .adk file onto my g1 phone.   I see it in the top-
level directory, but how can I execute it now?  I think I need to copy
it to  the system\app directory on the phone (where I see the rest of
the .adk files)... However, I dont know how to copy it there.  My
android file manager wont copy/paste it there.  How do I get my app
to  fireup on the g1 phone?


2. How to force portrait mode?


        I need to force the portrait layout on our app at all times, meaning  
when the phone is rotated to landscape orientation the view should  
appear sideways, whether or not the keyboard is out.  How to I enforce  

        BTW - I understand why this is undesirable but I'm not the UI  
designer nor the person that approved the layout.  I'm a humble coder  
so if you feel the urge to ignore the above question and address the  
bad design please move along.  I already know.

Thank you!



3. Setting "Use by default for this action" option crashes when attempting to replace a standard application

4. can not create Surface and ISurface successfully

5. posibility of self-acting sms/ mms an phone calls

6. Why isn't this layout file working?

7. Android Market's "Publish" Button does nothing