how to pause and stop a changing ImageView

Posted by user270811 on Stack Overflow See other posts from Stack Overflow or by user270811
Published on 2010-03-18T21:43:53Z Indexed on 2010/03/18 23:01 UTC
Read the original article Hit count: 308

Filed under:

hi,

i have an ImageView in which the picture switches every 5s, i am trying to add a pause and

resume button that can stop and restart the action. i am using a Handler, Runnable, and

postDelay() for image switch, and i put the code on onResume. i am thinking about using

wait and notify for the pause and resume, but that would mean creating an extra thread. so

far for the thread, i have this:

class RecipeDisplayThread extends Thread { boolean pleaseWait = false;

    // This method is called when the thread runs
    public void run() {
        while (true) {
            // Do work

            // Check if should wait
            synchronized (this) {
                while (pleaseWait) {
                    try {
                        wait();
                    } catch (Exception e) {
                    }
                }
            }

            // Do work
        }
    }

}   

and in the main activity's onCreate():

        Button pauseButton = (Button) findViewById(R.id.pause);

        pauseButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                while (true) {

                synchronized (thread) 
                {
                    thread.pleaseWait = true;
                    }
                }

            }
        });

        Button resumeButton = (Button) findViewById(R.id.resume);

        resumeButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                while (true) {

                // Resume the thread
                synchronized (thread) 
                {
                    thread.pleaseWait = false;
                    thread.notify();
                }
                }

            }
        });

the pause button seems to work, but then after that i can't press any other button, such as

the resume button.

thanks.

© Stack Overflow or respective owner

Related posts about android