У меня есть текстовое изображение, и я выделяю его динамически (первые 110 букв выделяются сначала, а затем через 1 секунду подсвечиваются следующие 110 букв и т. Д.). Ниже приведен мой код.Как остановить мой таймер через 3 секунды?
Я только что создал фоновый поток как таймер, но он не останавливается вообще. Как остановить таймер после 3 итераций? Спасибо заранее ...
int x=0;,y=110//global values
Timer timer = new Timer();
//Create a task which the timer will execute. This should be an implementation of the TimerTask interface.
//I have created an inner class below which fits the bill.
MyTimer mt = new MyTimer();
//We schedule the timer task to run after 1000 ms and continue to run every 1000 ms.
timer.schedule(mt, 1000, 1000);
class MyTimer extends TimerTask {
public void run() {
//This runs in a background thread.
//We cannot call the UI from this thread, so we must call the main UI thread and pass a runnable
if(x==330)
Thread.currentThread().destroy();
runOnUiThread(new Runnable() {
public void run() {
Spannable WordtoSpan = new SpannableString(names[0]);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), x, y, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x=x+110;
y=y+110;
textView.setText(WordtoSpan);
}
});
}
}
По крайней мере, прочитайте документацию для 'Timer' и' TimerTask'. поиск 'cancel'. – njzk2
Возможный дубликат [Как остановить таймер через определенное время?] (Http://stackoverflow.com/questions/15894731/how-to-stop-the-timer-after-certain-time) – Sufian