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?
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.