android substuting the click with a timer

我是研究僧i 提交于 2019-12-02 07:07:12

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);
   }
}
}

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."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!