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

@Override in ShakeDetector.java

Eclipse says that the two methods that are annotated with @Override need to override the superclass method. I thought they already were overriding. Should I delete the annotation?

Can you post the code you are talking about specifically?

(starts on line 41 of the ShakeDetector.java file)
@Override public void onSensorChanged(SensorEvent event) { // This method will be called when the accelerometer detects a change.

    // Call a helper method that wraps code from the Android developer site
    setCurrentAcceleration(event);

    // Get the max linear acceleration in any direction
    float maxLinearAcceleration = getMaxCurrentLinearAcceleration();

    // Check if the acceleration is greater than our minimum threshold
    if (maxLinearAcceleration > MIN_SHAKE_ACCELERATION) {
        long now = System.currentTimeMillis();

        // Set the startTime if it was reset to zero
        if (startTime == 0) {
            startTime = now;
        }

        long elapsedTime = now - startTime;

        // Check if we're still in the shake window we defined
        if (elapsedTime > MAX_SHAKE_DURATION) {
            // Too much time has passed. Start over!
            resetShakeDetection();
        }
        else {
            // Keep track of all the movements
            moveCount++;

            // Check if enough movements have been made to qualify as a shake
            if (moveCount > MIN_MOVEMENTS) {
                // It's a shake! Notify the listener.
                mShakeListener.onShake();

                // Reset for the next one!
                resetShakeDetection();
            }
        }
    }
}

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

your class should look EXACTLY like the one provided:

package name.of.your.package;

import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener;

public class ShakeDetector implements SensorEventListener {

private static final int MIN_SHAKE_ACCELERATION = 5;
private static final int MIN_MOVEMENTS = 2;
private static final int MAX_SHAKE_DURATION = 500;
private float[] mGravity = { 0.0f, 0.0f, 0.0f };
private float[] mLinearAcceleration = { 0.0f, 0.0f, 0.0f };

private static final int X = 0;
private static final int Y = 1;
private static final int Z = 2;

private OnShakeListener mShakeListener;
long startTime = 0;
int moveCount = 0;

public ShakeDetector(OnShakeListener shakeListener) {
    mShakeListener = shakeListener;
}

@Override
public void onSensorChanged(SensorEvent event) {
    setCurrentAcceleration(event);
    float maxLinearAcceleration = getMaxCurrentLinearAcceleration();
    if (maxLinearAcceleration > MIN_SHAKE_ACCELERATION) {
        long now = System.currentTimeMillis();
        if (startTime == 0) {
            startTime = now;
        }
        long elapsedTime = now - startTime;
        if (elapsedTime > MAX_SHAKE_DURATION) {
            resetShakeDetection();
        } else {
            moveCount++;
            if (moveCount > MIN_MOVEMENTS) {
                mShakeListener.onShake();
                resetShakeDetection();
            }
        }
    }
}

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

private void setCurrentAcceleration(SensorEvent event) {
    final float alpha = 0.8f;
    // Gravity components of x, y, and z acceleration
    mGravity[X] = alpha * mGravity[X] + (1 - alpha) * event.values[X];
    mGravity[Y] = alpha * mGravity[Y] + (1 - alpha) * event.values[Y];
    mGravity[Z] = alpha * mGravity[Z] + (1 - alpha) * event.values[Z];
    // Linear acceleration along the x, y, and z axes (gravity effects
    // removed)
    mLinearAcceleration[X] = event.values[X] - mGravity[X];
    mLinearAcceleration[Y] = event.values[Y] - mGravity[Y];
    mLinearAcceleration[Z] = event.values[Z] - mGravity[Z];
}

private float getMaxCurrentLinearAcceleration() {
    // Start by setting the value to the x value
    float maxLinearAcceleration = mLinearAcceleration[X];
    // Check if the y value is greater
    if (mLinearAcceleration[Y] > maxLinearAcceleration) {
        maxLinearAcceleration = mLinearAcceleration[Y];
    }
    // Check if the z value is greater
    if (mLinearAcceleration[Z] > maxLinearAcceleration) {
        maxLinearAcceleration = mLinearAcceleration[Z];
    }
    // Return the greatest value
    return maxLinearAcceleration;
}

private void resetShakeDetection() {
    startTime = 0;
    moveCount = 0;
}

public interface OnShakeListener {
    public void onShake();
}

}

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Check out this earlier post about this same issue: http://teamtreehouse.com/forum/adding-a-shake-detector

This appears to usually be a problem with project-specific compiler settings (read all the way to the bottom of the post).

Hope this helps!

Great! Thanks this works! After doing the changes you suggested, I also had to restart Eclipse.