Stop Android accelerometer after shaking

I want to listen to the shake and then completely stop the accelerometer and move on to another action. Unfortunately I haven't found any way to do this. Even if I count the variable and check with a simple "if" it always loads a new action every time a jitter is detected.

Please help me with my lack of understanding.

@Override
  protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    mSensorManager.unregisterListener(mSensorListener);
    super.onPause();

  }

  @Override
  protected void onStop() {
    mSensorManager.unregisterListener(mSensorListener);
    super.onStop();
  }




private SensorManager mSensorManager;

  private final SensorEventListener mSensorListener = new SensorEventListener() {


@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}




@Override
public void onSensorChanged(SensorEvent event) {


    float[] value = event.values;



    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = value[0];
            float y = value[1];
            float z = value[2];


            //in G Umrechnung
            float gX = x / gravityEarth;
            float gY = y / gravityEarth;
            float gZ = z / gravityEarth;

          //G-Force will be 1 when there is no movement. (gravity)
             gForce = FloatMath.sqrt(gX * gX + gY * gY + gZ * gZ);

             double gForce2 = Math.round(gForce * 100) / 100.0;

             String g = String.format("" + gForce2 + " G");
             TextViewG.setTextColor(0xffff0000);
             TextViewG.setTextSize(35);
             TextViewG.setText(g);

    }


    Intent nextScreen = new Intent(getApplicationContext(), Send.class);

    if (gForce > shakeThresholdInGForce ){


        final long now = System.currentTimeMillis();

        // ignore shake events too close to each other (500ms)
        if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now ) {

            mShakeTimestamp = now;



                   mSensorManager.unregisterListener(mSensorListener);
            onStop();




            startActivity(nextScreen);



        }

    }

      

+3


source to share





All Articles