In Eclipse, I found that using a TimeLine and a KeyFrame I can delay the execution of code, for example, to wait 4 seconds before playing a media I used this code:
Timeline timeline = new Timeline();
KeyFrame keyframe = new KeyFrame(Duration.millis(4000), DelayAnimation ->
{
GeneralMethods PlayMusic = new GeneralMethods();
PlayMusic.playMusic(mediaPlayer);
});
timeline.getKeyFrames().add(keyframe);
timeline.play();
The previous code works fine in Eclipse and I did not need to create a new thread.
My question is, Is there a similar way to delay the execution of code in Android?
I found a way to delay the execution of code, but it is using a handler, that is to say, creating a new thread. I would like to delay without using a thread, is it possible?
Handler is what you use to delay something in your thread. In the following case, the handler makes your UI TextView disappear after waiting for ten seconds;
private void makeTextViewDisappear(){
yourTV.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
yourTV.setVisibility(View.INVISIBLE);
// OR yourTV.setVisibility(View.GONE) to reclaim the space
}
}, 10000);
}
As you can see, there is no new thread.