How to loop through timer class for delayed time?
up vote
0
down vote
favorite
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask() {
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++) {
final int finalA = a;
handler.postDelayed(new Runnable() {
@Override
public void run() {
startTimer(30000);
}
}, 30000); // delay until to finish first timer for 30 sec
}
}
java android for-loop countdowntimer
New contributor
add a comment |
up vote
0
down vote
favorite
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask() {
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++) {
final int finalA = a;
handler.postDelayed(new Runnable() {
@Override
public void run() {
startTimer(30000);
}
}, 30000); // delay until to finish first timer for 30 sec
}
}
java android for-loop countdowntimer
New contributor
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a customRunnable
.
– Mark Keen
yesterday
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
yesterday
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and theHandler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.
– Mark Keen
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask() {
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++) {
final int finalA = a;
handler.postDelayed(new Runnable() {
@Override
public void run() {
startTimer(30000);
}
}, 30000); // delay until to finish first timer for 30 sec
}
}
java android for-loop countdowntimer
New contributor
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask() {
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++) {
final int finalA = a;
handler.postDelayed(new Runnable() {
@Override
public void run() {
startTimer(30000);
}
}, 30000); // delay until to finish first timer for 30 sec
}
}
java android for-loop countdowntimer
java android for-loop countdowntimer
New contributor
New contributor
edited yesterday
Kling Klang
32k156286
32k156286
New contributor
asked yesterday
Thippesh S
1
1
New contributor
New contributor
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a customRunnable
.
– Mark Keen
yesterday
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
yesterday
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and theHandler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.
– Mark Keen
yesterday
add a comment |
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a customRunnable
.
– Mark Keen
yesterday
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
yesterday
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and theHandler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.
– Mark Keen
yesterday
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a custom
Runnable
.– Mark Keen
yesterday
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a custom
Runnable
.– Mark Keen
yesterday
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
yesterday
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
yesterday
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and the
Handler
instances can cause short term memory leaks because the Runnable
instances have an implicit reference, which is held until processed, of the outer class.– Mark Keen
yesterday
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and the
Handler
instances can cause short term memory leaks because the Runnable
instances have an implicit reference, which is held until processed, of the outer class.– Mark Keen
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
To run a timer for n seconds you can use CountDownTimer
private void startTimer() {
new CountDownTimer(30000, 1000) {
int secondsLeft = 0;
public void onTick(long ms) {
if (Math.round((float) ms / 1000.0f) != secondsLeft) {
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("Seconds left( " + secondsLeft + " )");
}
}
public void onFinish() {
resend_timer.setClickable(true);
resend_timer.setText("30 sec finished");
}
}.start();
}
Now run this method on loop as per your need.
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
add a comment |
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer(){
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat){
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startTimer();
}
}, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
}
startTimerCount++;
}
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
To run a timer for n seconds you can use CountDownTimer
private void startTimer() {
new CountDownTimer(30000, 1000) {
int secondsLeft = 0;
public void onTick(long ms) {
if (Math.round((float) ms / 1000.0f) != secondsLeft) {
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("Seconds left( " + secondsLeft + " )");
}
}
public void onFinish() {
resend_timer.setClickable(true);
resend_timer.setText("30 sec finished");
}
}.start();
}
Now run this method on loop as per your need.
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
add a comment |
up vote
0
down vote
To run a timer for n seconds you can use CountDownTimer
private void startTimer() {
new CountDownTimer(30000, 1000) {
int secondsLeft = 0;
public void onTick(long ms) {
if (Math.round((float) ms / 1000.0f) != secondsLeft) {
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("Seconds left( " + secondsLeft + " )");
}
}
public void onFinish() {
resend_timer.setClickable(true);
resend_timer.setText("30 sec finished");
}
}.start();
}
Now run this method on loop as per your need.
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
To run a timer for n seconds you can use CountDownTimer
private void startTimer() {
new CountDownTimer(30000, 1000) {
int secondsLeft = 0;
public void onTick(long ms) {
if (Math.round((float) ms / 1000.0f) != secondsLeft) {
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("Seconds left( " + secondsLeft + " )");
}
}
public void onFinish() {
resend_timer.setClickable(true);
resend_timer.setText("30 sec finished");
}
}.start();
}
Now run this method on loop as per your need.
To run a timer for n seconds you can use CountDownTimer
private void startTimer() {
new CountDownTimer(30000, 1000) {
int secondsLeft = 0;
public void onTick(long ms) {
if (Math.round((float) ms / 1000.0f) != secondsLeft) {
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("Seconds left( " + secondsLeft + " )");
}
}
public void onFinish() {
resend_timer.setClickable(true);
resend_timer.setText("30 sec finished");
}
}.start();
}
Now run this method on loop as per your need.
answered yesterday
Satyajit Das
8615
8615
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
add a comment |
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
18 hours ago
add a comment |
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer(){
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat){
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startTimer();
}
}, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
}
startTimerCount++;
}
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
add a comment |
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer(){
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat){
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startTimer();
}
}, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
}
startTimerCount++;
}
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer(){
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat){
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startTimer();
}
}, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
}
startTimerCount++;
}
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer(){
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat){
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startTimer();
}
}, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
}
startTimerCount++;
}
answered yesterday
Rahul Sonpaliya
1365
1365
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
add a comment |
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
18 hours ago
add a comment |
Thippesh S is a new contributor. Be nice, and check out our Code of Conduct.
Thippesh S is a new contributor. Be nice, and check out our Code of Conduct.
Thippesh S is a new contributor. Be nice, and check out our Code of Conduct.
Thippesh S is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238170%2fhow-to-loop-through-timer-class-for-delayed-time%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a custom
Runnable
.– Mark Keen
yesterday
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
yesterday
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and the
Handler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.– Mark Keen
yesterday