Posted By

noloman on 02/23/13


Tagged

listener Sensor android shake


Versions (?)

Shake event listener


 / Published in: Java
 

URL: http://stackoverflow.com/a/5117254/257948

Shake event listener

  1. package com.xs2theworld.apptown.hardware;
  2.  
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7.  
  8. /**
  9.  * Listener that detects shake gesture.
  10.  */
  11. public class ShakeEventListener implements SensorEventListener {
  12.  
  13. /** Minimum movement force to consider. */
  14. private static final int MIN_FORCE = 10;
  15.  
  16. /**
  17. * Minimum times in a shake gesture that the direction of movement needs to
  18. * change.
  19. */
  20. private static final int MIN_DIRECTION_CHANGE = 3;
  21.  
  22. /** Maximum pause between movements. */
  23. private static final int MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 200;
  24.  
  25. /** Maximum allowed time for shake gesture. */
  26. private static final int MAX_TOTAL_DURATION_OF_SHAKE = 400;
  27.  
  28. /** Time when the gesture started. */
  29. private long mFirstDirectionChangeTime = 0;
  30.  
  31. /** Time when the last movement started. */
  32. private long mLastDirectionChangeTime;
  33.  
  34. /** How many movements are considered so far. */
  35. private int mDirectionChangeCount = 0;
  36.  
  37. /** The last x position. */
  38. private float lastX = 0;
  39.  
  40. /** The last y position. */
  41. private float lastY = 0;
  42.  
  43. /** The last z position. */
  44. private float lastZ = 0;
  45.  
  46. /** OnShakeListener that is called when shake is detected. */
  47. private OnShakeListener mShakeListener;
  48.  
  49. /**
  50. * Interface for shake gesture.
  51. */
  52. public interface OnShakeListener {
  53.  
  54. /**
  55. * Called when shake gesture is detected.
  56. */
  57. void onShake();
  58. }
  59.  
  60. public void setOnShakeListener(OnShakeListener listener) {
  61. mShakeListener = listener;
  62. }
  63.  
  64. @Override
  65. public void onSensorChanged(SensorEvent se) {
  66. // get sensor data
  67. float x = se.values[SensorManager.DATA_X];
  68. float y = se.values[SensorManager.DATA_Y];
  69. float z = se.values[SensorManager.DATA_Z];
  70.  
  71. // calculate movement
  72. float totalMovement = Math.abs(x + y + z - lastX - lastY - lastZ);
  73.  
  74. if (totalMovement > MIN_FORCE) {
  75.  
  76. // get time
  77. long now = System.currentTimeMillis();
  78.  
  79. // store first movement time
  80. if (mFirstDirectionChangeTime == 0) {
  81. mFirstDirectionChangeTime = now;
  82. mLastDirectionChangeTime = now;
  83. }
  84.  
  85. // check if the last movement was not long ago
  86. long lastChangeWasAgo = now - mLastDirectionChangeTime;
  87. if (lastChangeWasAgo < MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE) {
  88.  
  89. // store movement data
  90. mLastDirectionChangeTime = now;
  91. mDirectionChangeCount++;
  92.  
  93. // store last sensor data
  94. lastX = x;
  95. lastY = y;
  96. lastZ = z;
  97.  
  98. // check how many movements are so far
  99. if (mDirectionChangeCount >= MIN_DIRECTION_CHANGE) {
  100.  
  101. // check total duration
  102. long totalDuration = now - mFirstDirectionChangeTime;
  103. if (totalDuration < MAX_TOTAL_DURATION_OF_SHAKE) {
  104. mShakeListener.onShake();
  105. resetShakeParameters();
  106. }
  107. }
  108.  
  109. } else {
  110. resetShakeParameters();
  111. }
  112. }
  113. }
  114.  
  115. /**
  116. * Resets the shake parameters to their default values.
  117. */
  118. private void resetShakeParameters() {
  119. mFirstDirectionChangeTime = 0;
  120. mDirectionChangeCount = 0;
  121. mLastDirectionChangeTime = 0;
  122. lastX = 0;
  123. lastY = 0;
  124. lastZ = 0;
  125. }
  126.  
  127. @Override
  128. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  129. }
  130.  
  131. }
  132.  
  133. In our activity:
  134.  
  135. private SensorManager mSensorManager;
  136.  
  137. private ShakeEventListener mSensorListener;
  138.  
  139. in our onCreate():
  140.  
  141. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  142. mSensorListener = new ShakeEventListener();
  143.  
  144. mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
  145.  
  146. public void onShake() {
  147. Toast.makeText(KPBActivityImpl.this, "Shake!", Toast.LENGTH_SHORT).show();
  148. }
  149. });
  150.  
  151. Then:
  152.  
  153. @Override
  154. protected void onResume() {
  155. super.onResume();
  156. mSensorManager.registerListener(mSensorListener,
  157. mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  158. SensorManager.SENSOR_DELAY_UI);
  159. }
  160.  
  161. @Override
  162. protected void onPause() {
  163. mSensorManager.unregisterListener(mSensorListener);
  164. super.onStop();
  165. }

Report this snippet  

You need to login to post a comment.