我想在两行代码之间暂停一下,让我解释一下:

->用户点击一个按钮(事实上是一张卡片),我通过改变这个按钮的背景来显示它:

thisbutton.setBackgroundResource(R.drawable.icon);

->让我们说1秒后,我需要通过改变它的背景回到按钮的前一个状态:

thisbutton.setBackgroundResource(R.drawable.defaultcard);

我试着在这两行代码之间暂停线程:

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

然而,这是行不通的。也许这是进程,而不是线程,我需要暂停?

我也试过(但没用):

new Reminder(5);

用这个:

public class Reminder {

Timer timer;

        public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds*1000);
        }

        class RemindTask extends TimerTask {
            public void run() {
                System.out.format("Time's up!%n");
                timer.cancel(); //Terminate the timer thread
            }
        }  
    }

如何暂停/休眠线程或进程?


当前回答

我用这个:

Thread closeActivity = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      Thread.sleep(3000);
      // Do some stuff
    } catch (Exception e) {
      e.getLocalizedMessage();
    }
  }
});

其他回答

我用这个:

Thread closeActivity = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      Thread.sleep(3000);
      // Do some stuff
    } catch (Exception e) {
      e.getLocalizedMessage();
    }
  }
});

这是我的例子

创建一个Java Utils

    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.Intent;

    public class Utils {

        public static void showDummyWaitingDialog(final Context context, final Intent startingIntent) {
            // ...
            final ProgressDialog progressDialog = ProgressDialog.show(context, "Please wait...", "Loading data ...", true);

            new Thread() {
                public void run() {
                    try{
                        // Do some work here
                        sleep(5000);
                    } catch (Exception e) {
                    }
                    // start next intent
                    new Thread() {
                        public void run() {
                        // Dismiss the Dialog 
                        progressDialog.dismiss();
                        // start selected activity
                        if ( startingIntent != null) context.startActivity(startingIntent);
                        }
                    }.start();
                }
            }.start();  

        }

    }    

我知道这是一个旧的线程,但在Android文档中,我找到了一个非常适合我的解决方案…

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        mTextField.setText("done!");
    }
}.start();

https://developer.android.com/reference/android/os/CountDownTimer.html

希望这能帮助到某人…

你可能不想这么做。通过在单击按钮的事件处理程序中显式地放入sleep(),实际上会将整个UI锁定一秒钟。另一种选择是使用一些单发计时器。创建一个TimerTask将背景颜色更改为默认颜色,并在Timer上调度它。

另一种可能是使用Handler。有一个关于某人从使用定时器切换到使用处理程序的教程。

顺便说一句,您不能暂停一个进程。Java(或Android)进程至少有一个线程,并且只能休眠线程。

或者你可以用:

android.os.SystemClock.sleep(checkEvery)

它的优点是不需要包装尝试…抓住。