I have an app, which shows a video. At some point I want to give information about videos places. For example, historical places names. I am using this code:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
But when I use this postDelayed comment in many times some message not show or overtake. What is the best solution to show some texts in a delay? For example 5 seconds later show A text, 15 sec later show B text, 30 sec later show C text. my codes look like this:
switch ()
case 1:
openVideo(video1)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//my message
}
}, 4000);
break;
case 2:
openVideo(video2)
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
//my second message
}
}, 3000);
handler2.postDelayed(new Runnable() {
#Override
public void run() {
//my third message
}
}, 15000);
break;
Use multi-runnable.. Increase your duration for each text.
int mDuration=0,mAnimationDuration=5000;
mDuration=mDuration+mAnimationDuration;
//it called after 5 seconds
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your first text
}
}, mDuration);
mDuration=mDuration+mAnimationDuration;
//it called after 10 seconds
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your second text
}
}, mDuration);
mDuration=mDuration+mAnimationDuration;
//it called after 15 seconds
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your third text
}
}, mDuration);
Use Timer for every second...
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//put you code here
//or set switch case for time 5,10,15 seconds
}
}, 0, 1000);//put here time 1000 milliseconds=1 second
Related
This question already has answers here:
How to delay a loop in android without using thread.sleep?
(4 answers)
Closed 10 months ago.
I am trying to insert the handler to the loop but it makes it happen only once.
for(int i = 0;i<4;i++) {
btn1.setBackgroundColor(Color.RED);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 2000);
}
If you want the button to change between red and green with a delay of 2 seconds you can try something like this:
for(int i = 0;i<4;i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.RED);
}
}, 2000*2*i);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 2000*(2*i+1));
}
When using
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 2000)
you set the delay to a fixed value.
to make the button blink the delay has to be dynamic and changed every loop iteration.
Hi i have a button from which i call the given methods the problem is when i click button for the first time TextToSpeach Player doesn't play the sound but handler code execute perfectly.If i click on button again everything work perfectly means TextToSpeach play the sound.Thank you..
public void selectDest() {
TextToSpeechPlayer.playSound("hello");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.d(TAG, "After 1 sec ");
}
},1000);
}
Try this
TextToSpeechPlayer.playSound("hello");
handler = new Handler();
runnable = new Runnable() {
public void run() {
//do your other task here
handler.postDelayed(this, 60 * 1000);
}
};
handler.postDelayed(runnable, 0);
public void selectDest() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
TextToSpeechPlayer.playSound("hello");
Log.d(TAG, "After 1 sec ");
}
},1000);
}
I want to print the current second using a handler. I record a video for exactly 10 seconds and want to set the text of a TextView every second.
Recording 10 seconds works like that:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
}, 11000); // don't know why 11000 but it only works this way
After the 10 seconds the method stopRecordingVideo() gets executed. So how can I change the text every second of the TextView?
Working answer:
int t = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
t++;
textView.setText(getString(R.string.formatted_time, t));
if(t<10) {
handler.postDelayed(this, 1000);
}
}
}, 1000);
Where formatted_time is something like that:
<string android:name="formatted_time">%d seconds</string>
To print text every second, you can use CountDownTimer. But if you want to achieve this with try below code:
void startTime(){
//Post handler after 1 second.
handler.postDelayed(runnable, 1000);
}
int totalDelay=0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
totalDelay++;
if(totalDelay<=10){
//If total time is less then 10 second execute handler again after 1 second
handler.postDelayed(runnable, 1000);
}
textView.setText(totalDelay+" Second");
}
};
Try this, basically do the increment in a worker thread, but updating the text view is done by main's thread handler.
Thread worker= new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// stop recording after 10 seconds
if (i == 9) {
handler.post(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
});
}
else{
// set text for each second
handler.post(new Runnable() {
#Override
public void run() {
textView.setText(String.valueOf(i+1));
}
});
}
}//ends for()
worker.start()
Handler handler = new Handler();
if (v.getId() == R.id.play){
handler.postDelayed(new Runnable() {
public void run() {
play.setBackgroundResource(R.drawable.ilk);
}
}, 2000);
play.setText("Play");
}
I want to set background first and then after 2 seconds later, code will continue next line which is play.setText("Play"); and goes like that. Instead of this, first text appears. 2 seconds later background changes.
Handler.postDelayed returns immediately. And next line is executed.
After indicated milliseconds, the Runnable will be executed.
So your code should be like this:
void doFirstWork() {
Handler handler = new Handler();
if (v.getId() == R.id.play){
handler.postDelayed(new Runnable() {
public void run() {
play.setText("Play");
doNextWork();
}
}, 2000);
play.setBackgroundResource(R.drawable.ilk);
}
}
void doNextWork() {
...
}
Set the background first. After that set the text within Handler. As you've put delays at the end of postDelayed so it'll fire right after that stated delays or in your case after 2 sec.
if (v.getId() == R.id.play){
play.setBackgroundResource(R.drawable.ilk);
new Handler().postDelayed(new Runnable() {
public void run() {
play.setText("Play");
}
}, 2000);
}
I would like to know if it's possible to use handler().postdelayed twice?
I mean, I want to create a button, that when clicked it change the color and stay in this state 1 second, then, after 1 second another button change the color.
I've created the following code:
In the onclicklistener:
btn3.setBackgroundColor(Color.WHITE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
checkAnswer();
waitAnswer();
btnRsp3.setBackgroundResource(R.drawable.selector);
}
}, 1000);
CheckAnswer:
public void CheckAnswer(){
btn1.setBackgroundColor(Color.GREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
}
}, 500);
btn1.setBackgroundResource(R.drawable.selector);
}
I think the problem is on CheckAnswer because it seems it doesn't stop in this postDelayed and step to the waitAnswer.
Thanks
Why do you expect it to stop on postDelayed? postDelayed places your Runnable to the Handler Looper queue and returns. Since both handlers are created on the same looper, the second runnable is executed after the first one terminates (plus whatever left of the 500 ms delay)
UPDATE:
You need something like that
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 1000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundResource(R.drawable.selector);
}
}, 2000);
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
//Your Work
}
}, 1000);