Question

I don't get this :

In a ShakeListener class, I execute a routine in the containing class.

The routine is :

    public void showWord(){
        myShakeListener.stop();     

        flipper.showNext();

        v.vibrate(countdown5, -1);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        myShakeListener.start();
    }

Strange thing (to me, still a beginner), is that the thread sleeps BEFORE the next view is shown. Why is that?

What I want to accomplish : user shakes phone -> viewflipper flips to next -> Phone is unresponsive to shaking for 5 seconds -> user shakes phone -> viewflipper flips to next...

thnx

Was it helpful?

Solution

The problem is that the viewflipper is probably another thread. You're hitting a race condition. Better option is to spawn a thread for 5 seconds that sets a boolean called something like "noshake" to true when it starts and sets it false it when it's done. Check if noshake == false before allowing another shake.

Does that makes sense?

OTHER TIPS

It's because your code is blocking the UI thread. You should do something like this:

Handler mHandler = new Handler();
public void showWord(){
    myShakeListener.stop();     

    flipper.showNext();

    v.vibrate(countdown5, -1);

    mHandler.postAtTime(new Runnable() {
        @Override
        public void run() {
            myShakeListener.start();
        }
    }, 5000);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top