Вы можете попробовать это, это короткий
SystemClock.sleep(7000);
ПРЕДУПРЕЖДЕНИЕ : Никогда, никогда не делайте этого в потоке пользовательского интерфейса.
Используйте это, чтобы спать, например. фоновая нить.
Полное решение вашей проблемы будет:
Это доступно API 1
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View button) {
button.setBackgroundResource(R.drawable.avatar_dead);
final long changeTime = 1000L;
button.postDelayed(new Runnable() {
@Override
public void run() {
button.setBackgroundResource(R.drawable.avatar_small);
}
}, changeTime);
}
});
Без создания обработчика tmp. Также это решение лучше, чем @tronman, потому что мы не сохраняем представление Handler.
Также у нас нет проблем с обработчиком, созданным в плохом потоке;)
Документация
public static void sleep (long ms)
Добавлено на уровне API 1
Ожидает заданное количество миллисекунд (uptimeMillis) перед возвратом. Аналогичен режиму сна (long), , но не создает InterruptedException ; События interrupt () откладываются до
Следующая прерываемая операция.
не возвращает , пока не истечет хотя бы указанное количество миллисекунд.
Параметры
мс для сна перед возвращением, в миллисекундах времени безотказной работы.
Код для postDelayed из View class:
/**
* <p>Causes the Runnable to be added to the message queue, to be run
* after the specified amount of time elapses.
* The runnable will be run on the user interface thread.</p>
*
* @param action The Runnable that will be executed.
* @param delayMillis The delay (in milliseconds) until the Runnable
* will be executed.
*
* @return true if the Runnable was successfully placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting. Note that a
* result of true does not mean the Runnable will be processed --
* if the looper is quit before the delivery time of the message
* occurs then the message will be dropped.
*
* @see #post
* @see #removeCallbacks
*/
public boolean postDelayed(Runnable action, long delayMillis) {
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
return attachInfo.mHandler.postDelayed(action, delayMillis);
}
// Assume that post will succeed later
ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
return true;
}