处理程序/可运行与循环工作 [英] Handler/Runnable not working with loop
问题描述
我试图让我的TextView在一个单独的线程进行更新,以便它不会我的UI放缓。它的工作原理,但是当我在一个while循环来控制它添加,它在一个黑色的屏幕挂程序。
I am trying to get my TextView to update in a separate thread so that it doesn't slow down my UI. It works, but when I add in a while loop to control it, it hangs the program in a black screen.
code:
handler.post(new Runnable(){
@Override
public void run() {
while(media[6].isPlaying()) {
TextView myText = (TextView)findViewById(R.id.timerT);
myText.setText(getTimeString(media[1].getCurrentPosition()));
handler.postDelayed(this,10);
}
}
});
我以为可运行创建一个新的线程,这意味着这不会发生?还是我厉害了?
I thought that the runnable creates a new thread, meaning that this wouldn't happen? Or am I going badly wrong?
推荐答案
您黑屏,是因为你正在运行的任意时间while循环事件调度线程上:从来没有做到这一点。对于这种要使用的AsyncTask或启动的Runnable在一个单独的线程,然后用View.post只发送定时更新的东西。
Your black screen is because you are running an arbitrary duration while loop on the event dispatch thread: never do this. For something of this sort you want to use AsyncTask or start your Runnable in a separate thread and then use View.post to send the timer updates only.
这篇关于处理程序/可运行与循环工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!