Note that there are some explanatory texts on larger screens.

plurals
  1. POShake Listener detects orientation changes
    text
    copied!<p>I'm trying to detect shake events in my application so that I would be able to do something when user shakes the device. But the problem is; ShakeListener which I used, detects orientation changes as a shake event.(When I move the phone from landscape to portrait, it detects a shake)</p> <p>I tried changing the force threshold but at that time it can't detect any shake event.</p> <p>Here is the ShakeListener code which i took from <a href="http://android.hlidskialf.com/blog/code/android-shake-detection-listener" rel="nofollow">this site</a> :</p> <pre><code>import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class ShakeListener implements SensorEventListener { private static final int FORCE_THRESHOLD = 500; private static final int TIME_THRESHOLD = 100; private static final int SHAKE_TIMEOUT = 500; private static final int SHAKE_DURATION = 1000; private static final int SHAKE_COUNT = 3; private SensorManager mSensorMgr; private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f; private long mLastTime; private OnShakeListener mShakeListener; private Context mContext; private int mShakeCount = 0; private long mLastShake; private long mLastForce; public interface OnShakeListener { public void onShake(); } public ShakeListener(Context context) { mContext = context; } public void setOnShakeListener(OnShakeListener listener) { mShakeListener = listener; } public void resume(OnShakeListener listener) { this.setOnShakeListener(listener); mSensorMgr = (SensorManager) mContext .getSystemService(Context.SENSOR_SERVICE); if (mSensorMgr == null) { throw new UnsupportedOperationException("Sensors not supported"); } boolean supported = mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); if (!supported) { mSensorMgr.unregisterListener(this, mSensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER)); throw new UnsupportedOperationException( "Accelerometer not supported"); } } public void pause() { mShakeListener = null; if (mSensorMgr != null) { mSensorMgr.unregisterListener(this, mSensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER)); mSensorMgr = null; } } public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() != SensorManager.SENSOR_ACCELEROMETER) return; long now = System.currentTimeMillis(); if ((now - mLastForce) &gt; SHAKE_TIMEOUT) { mShakeCount = 0; } if ((now - mLastTime) &gt; TIME_THRESHOLD) { long diff = now - mLastTime; float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000; if (speed &gt; FORCE_THRESHOLD) { if ((++mShakeCount &gt;= SHAKE_COUNT) &amp;&amp; (now - mLastShake &gt; SHAKE_DURATION)) { mLastShake = now; mShakeCount = 0; if (mShakeListener != null) { mShakeListener.onShake(); } } mLastForce = now; } mLastTime = now; mLastX = event.values[SensorManager.DATA_X]; mLastY = event.values[SensorManager.DATA_Y]; mLastZ = event.values[SensorManager.DATA_Z]; } } } </code></pre> <p>I want to detect only shake movements, not orientation changes.</p> <p>Any suggestion?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload