Shake Detector in Android

Posted by Sachin Joshi
3
May 31, 2016
76 Views

maxresdefault.jpg


In Android, the most commonly used sensors are the accelerometer, it is used to measure acceleration. Here it is explained how to detect a shake motion.

Step 1. Create Class ShakeDetector that will extend SensorEventListener:-


Step 1(a) :-  Declare the required variable :-

  1. /*gForce Must be greater than 1G (one earth gravity unit).*/

  2. private static final float THRESHOLD_GRAVITY = 3.2F;

  3. private static final int SLOP_TIME_MS = 1000; /*This decide after how much interval shake will detect */

  4. private static final int COUNT_RESET_TIME_MS = 3000;

  5. private Double mAccelCurrent, gForce = 0.00, mAccelLast = 0.00, mAccel = 0.00;

  6. private OnShakeListener mListener;

  7. private long mShakeTimestamp;

  8. private int mShakeCount;


Step 1(b) :-  Override its function :-

In your onSensorChange() method, you determine whether it’s a shake or not.

Here we calculate the magnitude of the acceleration correctly and correctly applies a timeout between shake events.

  1. @Override

  2.   public void onAccuracyChanged(Sensor sensor, int accuracy) {

  3.       MyUtilities.showLog("On sensor" + sensor);

  4.       MyUtilities.showLog("On accuracy" + accuracy);

  5.   }

  6.   @Override

  7.   public void onSensorChanged(SensorEvent event) {

  8.       DecimalFormat twoDForm = new DecimalFormat("#.#");

  9.       if (mListener != null) {

  10.           float x = event.values[0];

  11.           float y = event.values[1];

  12.           float z = event.values[2];

  13.           float gravityX = x / SensorManager.GRAVITY_EARTH;

  14.           float gravityY = y / SensorManager.GRAVITY_EARTH;

  15.           float gravityZ = z / SensorManager.GRAVITY_EARTH;

  16.           // gForce==1, when there is no movement.

  17.           mAccelCurrent = gForce;

  18.           gForce = (Double) Math.sqrt(gX * gX + gY * gY + gZ * gZ);

  19.           Double delta = mAccelCurrent - mAccelLast;

  20.           mAccel = mAccel * 0.9f + delta;

  21.           mAccel = Double.valueOf(twoDForm.format(mAccel));

  22.           if (gForce >THRESHOLD_GRAVITY) {

  23.               final long now = System.currentTimeMillis();

  24.               // ignore shake events too close to each other (1000ms)

  25.               if (mShakeTimestamp + SLOP_TIME_MS > now) {

  26.                   return;

  27.               }

  28.               // reset the shake count after 3 seconds of no shakes

  29.               if (mShakeTimestamp + COUNT_RESET_TIME_MS < now) {

  30.                   mShakeCount = 0;

  31.               }

  32.               mShakeTimestamp = now;

  33.               mShakeCount++;

  34.               mListener.onShake(mShakeCount);

  35.           }

  36.       }

  37.   }


Read more about the Shake Detector in Android visit Findnerd.

1 people like it
avatar
Comments
avatar
Please sign in to add comment.