How to make an Android program 'wait'

后端 未结 8 1968
心在旅途
心在旅途 2020-12-16 02:23

I want to cause my program to pause for a certain number of milliseconds, how exactly would I do this?

I have found different ways such as Thread.sleep( time )

相关标签:
8条回答
  • 2020-12-16 02:52

    I think Thread.sleep(....) probably is what you want you just may not be doing it correctly. What exactly are you trying to pause? Hopefully not the UI thread? Im guessing you have some background thread performing some tasks at some kind of interval? If so then Thread.sleep will put that particular thread to sleep for a certain time, it will not however pause all threads.

    0 讨论(0)
  • 2020-12-16 02:53

    Do something like the following. Here is a link to the reference, this might be helpful.

    final MyActivity myActivity = this;   
    
        thread=  new Thread(){
            @Override
            public void run(){
                try {
                    synchronized(this){
                        wait(3000);
                    }
                }
                catch(InterruptedException ex){                    
                }
    
                // TODO              
            }
        };
    
        thread.start();   
    
    0 讨论(0)
提交回复
热议问题