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
}
}









share|improve this question









New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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 the Runnable instances have an implicit reference, which is held until processed, of the outer class.
    – Mark Keen
    yesterday

















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
}
}









share|improve this question









New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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 the Runnable instances have an implicit reference, which is held until processed, of the outer class.
    – Mark Keen
    yesterday















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
}
}









share|improve this question









New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Kling Klang

32k156286




32k156286






New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Thippesh S

1




1




New contributor




Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Thippesh S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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 the Runnable 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












  • 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


















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














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.






share|improve this answer





















  • 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




















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++;
}





share|improve this answer





















  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Thippesh S is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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
































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.






share|improve this answer





















  • 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

















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.






share|improve this answer





















  • 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















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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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














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++;
}





share|improve this answer





















  • 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

















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++;
}





share|improve this answer





















  • 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















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++;
}





share|improve this answer












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++;
}






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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












Thippesh S is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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




















































































Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation