Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait for 3 seconds or user click

Tags:

android

I am trying to set up a situation where I am waiting for a small period of time say 3 seconds and move on. But if the user clicks my on-screen button then move on as I would have anyway. Both events will trigger the same behaviour namely to update text on the screen. Any thoughts??

New to Android but mclovin' it

Thanks in advance

like image 974
codemanusa Avatar asked Apr 26 '11 13:04

codemanusa


1 Answers

Try something like this:

private Thread thread;    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutxml);

    final MyActivity myActivity = this;   

    thread=  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(3000);
                }
            }
            catch(InterruptedException ex){                    
            }

            // TODO              
        }
    };

    thread.start();        
}

@Override
public boolean onTouchEvent(MotionEvent evt)
{
    if(evt.getAction() == MotionEvent.ACTION_DOWN)
    {
        synchronized(thread){
            thread.notifyAll();
        }
    }
    return true;
}    

It waits 3 seconds to continue but if the user touches the screen the thread is notified and it stops waiting.

like image 86
ferostar Avatar answered Oct 13 '22 00:10

ferostar