2012-12-05 118 views
0

我有一個搖聽衆在我的活動之一在使用此偵聽如下安卓:Shakelistener錯誤

public class ShakeListener implements SensorEventListener { 
    private String TAG = ShakeListener.class.getSimpleName(); 
    private static final int FORCE_THRESHOLD = 800; 
    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 = 5; 

    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) { 

     Log.d(TAG,"ShakeListener invoked---->"); 
     mContext = context; 
     resume(); 
    } 

    public void setOnShakeListener(OnShakeListener listener) { 

     mShakeListener = listener; 
    } 

    public void resume() { 
     mSensorMgr = (SensorManager) mContext 
       .getSystemService(Context.SENSOR_SERVICE); 
     if (mSensorMgr == null) { 
      throw new UnsupportedOperationException("Sensors not supported"); 
     } 
     boolean supported = false; 
     try { 
      supported = mSensorMgr.registerListener(this, 
        mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
        SensorManager.SENSOR_DELAY_GAME); 
     } catch (Exception e) { 
      Toast.makeText(mContext, "Shaking not supported", Toast.LENGTH_LONG) 
        .show(); 
     } 

     if ((!supported) && (mSensorMgr != null)) 
      mSensorMgr.unregisterListener(this); 
    } 

    public void pause() { 
     if (mSensorMgr != null) { 

      mSensorMgr.unregisterListener(this); 
      mSensorMgr = null; 
     } 
    } 

    public void onAccuracyChanged(Sensor sensor, int accuracy) { 

    } 

    public void onSensorChanged(SensorEvent event) { 
     if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) 
      return; 
     long now = System.currentTimeMillis(); 

     if ((now - mLastForce) > SHAKE_TIMEOUT) { 
      mShakeCount = 0; 
     } 

     if ((now - mLastTime) > 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 > FORCE_THRESHOLD) { 
       if ((++mShakeCount >= SHAKE_COUNT) 
         && (now - mLastShake > SHAKE_DURATION)) { 
        mLastShake = now; 
        mShakeCount = 0; 
        Log.d(TAG,"ShakeListener mShakeListener---->"+mShakeListener); 
        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]; 
     }}} 

IAM作爲ffollows

mShaker = new ShakeListener(this); 
     mShaker.setOnShakeListener(new ShakeListener.OnShakeListener() { 
     public void onShake() 
      { 

       Intent myIntent = new Intent(myActivity.this, loginActivity.class); 
       startActivity(myIntent); 
      } 

     }); 

這種抖動應該只觸發時亞姆在那個特定的活動..但它觸發所有活動,更糟的是,即使當我關閉應用程序,如果我動搖,事件觸發並使應用程序打開..任何幫助表示讚賞。

回答

1

在您的活動添加這個方法:

@Override 
    protected void finalize() throws Throwable { 
     try { 
      stop(); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
     super.finalize(); 
    } 

    public void stop(){ 
     try { 
      sensorMgr.unregisterListener(this, mAccelerometer); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
+0

我應該在哪裏在監聽器添加或我的活動 – teekib

+0

檢查編輯請 – Sharmilee

+0

我加入你的代碼...還是同樣的事情:(,什麼是停止() ; – teekib