ShakeListener

Quickly share your Android Code Snippets here...

ShakeListener

Postby Terrormaster » Sat Feb 27, 2010 5:31 am

While working on my doodle app I thought it'd be cool to do an Etch-a-Sketch shake to erase kinda thing. I stumbled on a post here by snctln that demonstrates how to detect a shake using the accelerometer. The post was very informative and I was able to get it working in my app the way I wanted with little to no effort.

Being the type that likes to encapsulate things and make them reusable later down the line I thought wouldn't it be neat if this snippet say inside it's own listener that I could just implement in my activities? So I put this together and thought I'd share it with the folks here:

ShakeListener.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.util.List;
  2.  
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7.  
  8. public class ShakeListener implements SensorEventListener{
  9.     private static final String TAG = "ShakeListener";
  10.  
  11.         private OnShakeListener mOnShakeListener = null;  
  12.     private SensorManager mSensorManager;
  13.         private double mTotalForcePrev; // stores the previous total force value
  14.         private double mForceThreshHold = 1.5f;
  15.  
  16.         private List<Sensor> mSensors;
  17.         private Sensor mAccelerationSensor;
  18.  
  19.         public ShakeListener(SensorManager sm){
  20.                 mSensorManager = sm;
  21.                
  22.         mSensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  23.         if(mSensors.size() > 0) {
  24.                 mAccelerationSensor = mSensors.get(0);
  25.                 mSensorManager.registerListener(this, mAccelerationSensor, SensorManager.SENSOR_DELAY_GAME);
  26.         }
  27.        
  28.         }
  29.        
  30.         public void setForceThreshHold(double threshhold){
  31.                 mForceThreshHold = threshhold;
  32.         }
  33.         public double getForceThreshHold(){
  34.                 return mForceThreshHold;
  35.         }
  36.        
  37.         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  38.        
  39.     public void onSensorChanged(SensorEvent event) {    
  40.           double totalForce = 0.0f;
  41.           totalForce += Math.pow(event.values[0]/SensorManager.GRAVITY_EARTH, 2.0);
  42.           totalForce += Math.pow(event.values[1]/SensorManager.GRAVITY_EARTH, 2.0);
  43.           totalForce += Math.pow(event.values[2]/SensorManager.GRAVITY_EARTH, 2.0);
  44.           totalForce = Math.sqrt(totalForce);
  45.            
  46.           if((totalForce < mForceThreshHold) && (mTotalForcePrev > mForceThreshHold)) {
  47.                   OnShake(); // raise the onShake event.
  48.           }
  49.            
  50.           mTotalForcePrev = totalForce;
  51.     }
  52.    
  53.     public void setOnShakeListener(OnShakeListener listener) {
  54.         mOnShakeListener = listener;
  55.     }  
  56.  
  57.     private void OnShake(){  
  58.         if(mOnShakeListener!=null) {  
  59.                 mOnShakeListener.onShake();  
  60.         }  
  61.     }  
  62.    
  63.     public interface OnShakeListener {  
  64.         public abstract void onShake();  
  65.     }      
  66.    
  67. }      
  68.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


And to implement it in an Activity just use the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ShakeListener MyShake = new ShakeListener((SensorManager) getSystemService(SENSOR_SERVICE));
  2. MyShake.setForceThreshHold(1.9);
  3. MyShake.setOnShakeListener(new ShakeListener.OnShakeListener() {
  4.         @Override
  5.         public void onShake() {
  6.                 mPad.Clear();
  7.         }
  8. });
  9.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


You can use MyShake.setForceThreshHold(1.9) to set the sensitivity of the shake.

Enjoy,
-TM

EDIT: Updated for API Level 3.
You wouldn't by any chance have any silver bullets?
User avatar
Terrormaster
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Feb 02, 2010 7:29 pm
Location: New England

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 5 guests