Depreciated Accelerometer Examples

Put your problem here if it does not fit any of the other categories.

Depreciated Accelerometer Examples

Postby QAguy » Sat Jun 05, 2010 2:02 am

I am trying to find out how to access the accelerometer but all of the examples I have found use the depriciated SensorListener instead of SensorEventListener. I really need to find an example that CLEARLY shows the interaction between this class and sensor manager. I'm very new to developing on android as well as java so the more simple the better. Help is greatly appreciated!
QAguy
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jun 05, 2010 1:56 am

Top

Re: Depreciated Accelerometer Examples

Postby phix » Sat Jun 05, 2010 7:41 pm

I hope the author doesn't mind, but I found this one and had the same problem. Little research lead to a solution.

http://android.hlidskialf.com/blog/code ... n-listener
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package geerdesigns.android.NightLight.Services;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorEvent;
  7. import android.hardware.SensorEventListener;
  8. import android.hardware.SensorManager;
  9. import android.preference.PreferenceManager;
  10.  
  11. public class ShakeListener {
  12.         private static final String TAG = "ShakeListener";
  13.         private static final int TIME_THRESHOLD = 80;
  14.         private static final int SHAKE_TIMEOUT = 10000;
  15.         private static final int SHAKE_DURATION = 500;
  16.         private static final int SHAKE_COUNT = 4;
  17.        
  18.         private int FORCE_THRESHOLD = 1200;
  19.  
  20.         private SensorManager sMgr;
  21.         private OnShakeListener mShakeListener;
  22.         private SensorEventListener mListen;
  23.         private Sensor acc;
  24.         private Context ctx;
  25.        
  26.         private int mShakeCount = 0;
  27.         private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f;
  28.         private long mLastTime, mLastShake, mLastForce;
  29.         private boolean reg = false;
  30.        
  31.         public interface OnShakeListener {
  32.                 public void onShake();
  33.         }
  34.         public ShakeListener(Context context) {
  35.                 ctx = context;
  36.                 onResume();
  37.         }
  38.         public void setOnShakeListener(OnShakeListener l) {
  39.                 mShakeListener = l;
  40.         }
  41.         public void onResume() {
  42.                 sMgr = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
  43.                 acc = sMgr.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
  44.                 mListen = new SensorEventListener(){
  45.                        
  46.                         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  47.                         public void onSensorChanged(SensorEvent event) {
  48.                                 if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
  49.                                         return;
  50.                                 long now = System.currentTimeMillis();
  51.  
  52.                                 if ((now - mLastForce) > SHAKE_TIMEOUT)
  53.                                         mShakeCount = 0;
  54.  
  55.                                 if ((now - mLastTime) > TIME_THRESHOLD) {
  56.                                         long diff = now - mLastTime;
  57.                                         float speed = Math.abs(event.values[0] + event.values[1] + event.values[2] - mLastX - mLastY - mLastZ) / diff * 10000;
  58.  
  59.                                         if (speed > FORCE_THRESHOLD) {
  60.                                                 if ((++mShakeCount >= SHAKE_COUNT)      && (now - mLastShake > SHAKE_DURATION)) {
  61.                                                         mLastShake = now;
  62.                                                         mShakeCount = 0;
  63.                                                         if (mShakeListener != null)
  64.                                                                 mShakeListener.onShake();
  65.                                                 }
  66.                                                 mLastForce = now;
  67.                                         }
  68.                                         mLastTime = now;
  69.                                         mLastX = event.values[0];
  70.                                         mLastY = event.values[1];
  71.                                         mLastZ = event.values[2];
  72.                                 }
  73.                         }
  74.                 };
  75.                 reg = sMgr.registerListener(mListen, acc, SensorManager.SENSOR_DELAY_NORMAL);
  76.         }
  77.  
  78.         public void onPause() {
  79.                 sMgr.unregisterListener(mListen);
  80.                 sMgr = null;
  81.                 reg = false;
  82.                 mListen = null;
  83.         }
  84.         /*
  85.          * Getters/Setters
  86.          */
  87.         public long getThreshold() {
  88.                 return FORCE_THRESHOLD;
  89.         }
  90.         public void setThreshold(int newThresh){
  91.                 FORCE_THRESHOLD = newThresh;
  92.         }
  93.         public String getSensorName() {
  94.                 return acc.getName();
  95.         }
  96.         public int getSensorType() {
  97.                 return acc.getType();
  98.         }
  99.         public boolean isRegistered() {
  100.                 return reg;
  101.         }
  102. }
  103.  
  104.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 14 guests