Is this how to schedule a java method to run 1 second later?

后端 未结 4 729
日久生厌
日久生厌 2021-02-05 17:34

In my method, I want to call another method that will run 1 second later. This is what I have.

final Timer timer = new Timer();

timer.schedule(new TimerTask() {         


        
4条回答
  •  长发绾君心
    2021-02-05 17:59

    ScheduledExecutorService or AsyncTask for UI related.

    Note that if you are to update UI, that code should be posted to UI thread. as in Processes and Threads Guide

            final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
    

    There is also nice postDelayed method in View

        mImageView.postDelayed(new Runnable(){
            @Override
            public void run() {
                mImageView.setImageResource(R.drawable.ic_inactive);
            }
        }, 1000);
    

    that will update UI after 1 sec.

提交回复
热议问题