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
android listener accelerometer shake


source to share


No one has answered this question yet

See similar questions:

-1
How do I stop the accelerometer sensor?

or similar:

3606
Close / hide Android soft keyboard
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
2735
Stop EditText from getting focus when Activity starts
2609
Is there a unique identifier for an Android device?
2510
How to persist android activity state by persisting instance state?
2097
Is there a way to run Python on Android?
1844
What is "Context" on Android?
3
How to read heart rate from Android Wear
1
Android Intent Service injects SensorEventListener



All Articles
Loading...
X
Show
Funny
Dev
Pics