Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Shake Detector in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 476
    Comment on it

    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 :-

    /*gForce Must be greater than 1G (one earth gravity unit).*/
    
    private static final float THRESHOLD_GRAVITY = 3.2F;
    private static final int SLOP_TIME_MS = 1000; /*This decide after how much interval shake will detect */
    private static final int COUNT_RESET_TIME_MS = 3000;
    private Double mAccelCurrent, gForce = 0.00, mAccelLast = 0.00, mAccel = 0.00;
    
    private OnShakeListener mListener;
    private long mShakeTimestamp;
    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.

     @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            MyUtilities.showLog("On sensor" + sensor);
            MyUtilities.showLog("On accuracy" + accuracy);
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            DecimalFormat twoDForm = new DecimalFormat("#.#");
            if (mListener != null) {
                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];
                float gravityX = x / SensorManager.GRAVITY_EARTH;
                float gravityY = y / SensorManager.GRAVITY_EARTH;
                float gravityZ = z / SensorManager.GRAVITY_EARTH;
    
                // gForce==1, when there is no movement.
                mAccelCurrent = gForce;
                gForce = (Double) Math.sqrt(gX * gX + gY * gY + gZ * gZ);
                Double delta = mAccelCurrent - mAccelLast;
                mAccel = mAccel * 0.9f + delta;
                mAccel = Double.valueOf(twoDForm.format(mAccel));
                if (gForce >THRESHOLD_GRAVITY) {
                    final long now = System.currentTimeMillis();
                    // ignore shake events too close to each other (1000ms)
                    if (mShakeTimestamp + SLOP_TIME_MS > now) {
                        return;
                    }
                    // reset the shake count after 3 seconds of no shakes
                    if (mShakeTimestamp + COUNT_RESET_TIME_MS < now) {
                        mShakeCount = 0;
                    }
                    mShakeTimestamp = now;
                    mShakeCount++;
                    mListener.onShake(mShakeCount);
    
                }
    
            }
        }

     

    Step 2. Create interface called in your main activity when shaked is detected.

    public interface onShakeListener {
        public void onShake(int count);
    }

     

    Step 3. Call the function on your main activity

    To initialise the shake detector

    private void initialiseDetector() {
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

     

    Step 4:- Implement the shake listener and override its method.

    @Override
    public void onShake(int count) {
        Toast.makeText(Post.this, "Shake Count--"+count, Toast.LENGTH_LONG).show();
    }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: