title: How to detect shake and toast a message after shaking the phone 3 times in android
body: <pre><code>package com.example.womenssafety;
  import androidx.appcompat.app.AppCompatActivity;
  import android.content.Context;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.os.Bundle;
  import android.widget.Toast;
  public class MainActivity extends AppCompatActivity {
  private SensorManager sm;
  private float acelVal,acelLast,shake;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          sm=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
          sm.registerListener(sensorListener,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  
          acelVal=SensorManager.GRAVITY_EARTH;
          acelLast=SensorManager.GRAVITY_EARTH;
          shake=0.00f;
      }
  private final SensorEventListener sensorListener = new SensorEventListener() {
      @Override
      public void onSensorChanged(SensorEvent event) {
          float x =event.values[0];
          float y =event.values[1];
          float z =event.values[2];
          acelLast=acelVal;
          acelVal=(float) Math.sqrt((double) (x*x)+(y*y)+(z*z));
          float delta= acelVal-acelLast;
          shake =shake*0.9f+delta;
  
  
          if(shake&gt;12){
              Toast t =Toast.makeText(getApplicationContext(), &quot;Dont Shake phone&quot;,Toast.LENGTH_LONG);
              t.show();
          }
      }
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
      }
  };
  }
  </code></pre>
  <p>In this code it's toasting the message after shaking the phone 1 time. But I want to get the toast message after shaking the phone 3 times.</p>
  <p>How can I do this?</p>