android substuting the click with a timer

前端 未结 2 824
我在风中等你
我在风中等你 2021-01-24 14:33

I found this example and i have a small question: How can I remove the on click to a timer or a delay where it displays the first image and waits a couple of seconds then moves

相关标签:
2条回答
  • 2021-01-24 15:05

    No need for threads. Just use a Handler with postDelayed messages...

    public class HelloHandler extends Activity {
    
    protected Handler handler = new Handler();
    
    @Override
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           // blah blah blah
    
           handler.postDelayed(new UpdateTask(),500);
       }
    
    
       protected class UpdateTask implements Runnable {
       public void run() {
          // Do stuff.  This is UI thread.
          handler.postDelayed(this, 500);
       }
    }
    }
    
    0 讨论(0)
  • 2021-01-24 15:26

    Since blocking the UI thread is fundamentally against the Android framework, what you'd probably have to do is set up a Thread and Handler to sleep on another thread in a loop and use the Handler to pass messages back to the UI Thread and change the image. I know I had a good Thread/Handler example around here somewhere...

    Ah yes... have a look at the example on how to update a ProgressDialog through a Thread and Handler. It should give you some ideas.

    http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog

    (see the expandable section "Example ProgressDialog with a second thread."

    0 讨论(0)
提交回复
热议问题