Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Shake Detector

Hi, My app seems to detect a shake as soon as the onResume() method is call, so every time you back out of the app then go back into it, it makes the sound and displays an answer. How could i stop this happening?? I've been looking at the ShakeDetector class and thinking maybe change the startTime variable which is 0 to maybe 1 or something. I've also looked at the onResume() method, thinking that a flush or reset( type of method as it doesn't exist for the class) method could be used to get rid of any data store in the accelerometer sensor.

Any ideas anyone??

Thank you in advance.

3 Answers

You could try using a count variable to check whether the shake detection should trigger. Make a class variable like mCount or something. In onCreate() set it to 0. in onPause() set it to 0. When a shake is detected check to see if mCount == 0, if so don't perform action and increase the mCount value. If it is not 0 then perform action.

If this solution always stops your first shake then you could also try stopping the shake detection from happening during the first second of resuming by starting a timer in the onResume() function.

Hope this helps.

Cheers again Lawrence, You hit the nail on the head, I set a member variable at the top, then put a if statement before the call to a newAnswer() method and increased the value by 1 outside of the if statement. and then following the activity lifecycle, i then set it to 0 in the onResume() method. Here's the code for anyone who finds this post.

// member variable
private int mCount;

// shake detector inside a loop to stop activation on creation.
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector(new OnShakeListener() {
                @Override
                public void onShake() {
                    if(mCount != 0) {
                        handleNewAnswer();
                    }
                    mCount++;
                }   
            });

    @Override
    public void onResume() {
        super.onResume();
        mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
        mCount = 0;
    }

Thank you Lawrence, I've only just got your message. I'll have a muck around with those ideas.

Hi guys,

I seem to have the same issue as you Frank. I can solve it with a count variable, but that feels awfully like a weak band-aid. Is the behaviour normal and common for the listener? I think the device should not think it is being called with each resume.

Would you agree? Or should I just put the band aid and look away?

Cheers, Pablo