mercredi 27 février 2019

If statement using a runnable handler

I am creating an android application and I am adding a shake feature using the library seismic https://github.com/square/seismic

With this shake feature I am trying to make it so that depending on the count of each shake it will open a respective page i.e. shake once for page 1, shake twice for page 2 and so on.

I am using a handler to wait 1700ms before initiating the if statement so it knows the correct count rather than straight away going to the first if statement (when count is 1) but it still goes from page 1 to page 2 to page 3 rather than waiting and going to the correct page the first time.

Home Class

mShakeDetector.setOnShakeListener(new com.example.name.project.ShakeDetector.OnShakeListener() {

            @Override
            public void onShake(final int count) {
                System.out.println(count);

                final Intent tts = new Intent(context, ttsScreen.class);
                final Intent stt = new Intent(context, sttScreen.class);
                final Intent cbb = new Intent(context, cbbScreen.class);
                final Intent ocr = new Intent(context, ocrScreen.class);

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        if( count == 1 ) {
                            startActivity(tts);
                        }
                        else if (count == 2) {
                            startActivity(stt);
                        }
                        else if (count == 3) {
                            startActivity(cbb);
                        }
                        else if (count == 4) {
                            startActivity(ocr);
                        }
                    }
                }, 1800);



            }
        });

ShakeDetector Class

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;

public class ShakeDetector implements SensorEventListener {

    /*
     * The gForce that is necessary to register as shake.
     * Must be greater than 1G (one earth gravity unit).
     * You can install "G-Force", by Blake La Pierre
     * from the Google Play Store and run it to see how
     *  many G's it takes to register a shake
     */
    private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
    private static final int SHAKE_SLOP_TIME_MS = 500;
    private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;

    private OnShakeListener mListener;
    private long mShakeTimestamp;
    private int mShakeCount;

    public void setOnShakeListener(OnShakeListener listener) {
        this.mListener = listener;
    }

    public interface OnShakeListener {
        public void onShake(int count);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // ignore
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (mListener != null) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            float gX = x / SensorManager.GRAVITY_EARTH;
            float gY = y / SensorManager.GRAVITY_EARTH;
            float gZ = z / SensorManager.GRAVITY_EARTH;

            // gForce will be close to 1 when there is no movement.
            float gForce = (float)Math.sqrt( gX * gX + gY * gY + gZ * gZ );


            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                final long now = System.currentTimeMillis();
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return;
                }

                // reset the shake count after 3 seconds of no shakes
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0;
                }

                mShakeTimestamp = now;
                mShakeCount++;

                mListener.onShake(mShakeCount);
            }
        }
    }
}

So essentially the shake detector class registers shake counts for 3 seconds before resetting which is fine but now in my Home class, I want it to register my shakes wait X seconds and then open the class properly but I'm not sure what is wrong with my approach?

I added a system out of the count, and its detecting shakes fine

I/System.out: 1 I/System.out: 2 I/System.out: 3

But it goes from page 1 to page 2 to page 3 (you can see the transitions); what is the reason for this?

Aucun commentaire:

Enregistrer un commentaire