How to restart Handler / Runnable on method call? The Next CEO of Stack OverflowHow do I efficiently iterate over each entry in a Java Map?How do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Activity restart on rotation Android“implements Runnable” vs “extends Thread” in JavaWhy is the Android emulator so slow? How can we speed up the Android emulator?Can't create handler inside thread that has not called Looper.prepare()How do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?
How to get the last not-null value in an ordered column of a huge table?
Is there such a thing as a proper verb, like a proper noun?
It is correct to match light sources with the same color temperature?
Do scriptures give a method to recognize a truly self-realized person/jivanmukta?
Can Sneak Attack be used when hitting with an improvised weapon?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Does the Idaho Potato Commission associate potato skins with healthy eating?
Airplane gently rocking its wings during whole flight
Can you teleport closer to a creature you are Frightened of?
Is it ever safe to open a suspicious HTML file (e.g. email attachment)?
Is there an equivalent of cd - for cp or mv
Which Pokemon have a special animation when running with them out of their pokeball?
How to avoid supervisors with prejudiced views?
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
Is it professional to write unrelated content in an almost-empty email?
What would be the main consequences for a country leaving the WTO?
Computationally populating tables with probability data
Why don't programming languages automatically manage the synchronous/asynchronous problem?
Strange use of "whether ... than ..." in official text
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Man transported from Alternate World into ours by a Neutrino Detector
Yu-Gi-Oh cards in Python 3
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
What flight has the highest ratio of timezone difference to flight time?
How to restart Handler / Runnable on method call?
The Next CEO of Stack OverflowHow do I efficiently iterate over each entry in a Java Map?How do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Activity restart on rotation Android“implements Runnable” vs “extends Thread” in JavaWhy is the Android emulator so slow? How can we speed up the Android emulator?Can't create handler inside thread that has not called Looper.prepare()How do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?
I am using a method to instantiate a new Handler object and kicking off a method to do some task in the allocated time. The problem is, this timer is not restarted for each method call and any lingering time is continued with subsequent calls.
I am wondering how to fix this. This is my method:
private void doTask()
viewModel.getFeedBackMessage().set("Calculating ...");
viewModel.fetchData();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
if (viewModel.getData() == null)
viewModel.getFeedbackMessage().set("Failure.");
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
else
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
, 90000);
Edit:
new Thread(() ->
int elapsedTime = 0;
while (elapsedTime < 90000)
try
Thread.sleep(1000);
catch (Exception ex)
if (currentLocation != null)
break;
elapsedTime += 1000;
).start();
java android multithreading
|
show 4 more comments
I am using a method to instantiate a new Handler object and kicking off a method to do some task in the allocated time. The problem is, this timer is not restarted for each method call and any lingering time is continued with subsequent calls.
I am wondering how to fix this. This is my method:
private void doTask()
viewModel.getFeedBackMessage().set("Calculating ...");
viewModel.fetchData();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
if (viewModel.getData() == null)
viewModel.getFeedbackMessage().set("Failure.");
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
else
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
, 90000);
Edit:
new Thread(() ->
int elapsedTime = 0;
while (elapsedTime < 90000)
try
Thread.sleep(1000);
catch (Exception ex)
if (currentLocation != null)
break;
elapsedTime += 1000;
).start();
java android multithreading
1
Can you explain more? Why are you call remove callback and post delayed in both cases if/else. Removed is meant to cancel a future task or that future task is already being executed. So, the remove callback call is useless in my opinion.
– Hichem BOUSSETTA
Mar 8 at 18:06
Basically, if the data was found before the allocated time, I want to stop the Handler and if it doesn't find the data, then I still want to stop the Handler
– Darnold14
Mar 8 at 18:09
What you are posting is a delayed task that will be executed once instead you stop it before. But you can not stop the task inside the task itself. The remove callback has to be outside the delayed task, unless I am missing something.
– Hichem BOUSSETTA
Mar 8 at 18:19
How can I execute this timer task every method call?
– Darnold14
Mar 8 at 18:22
1
Okay I see. You are then using a wrong approach. In this type of scenario, you can use a do/while loop in which you check at each iteration if you have data available (this check must be short and non blocking), and then sleep the thread for some interval (let's say one second), after sleep you increment an elapsed integer variable that keeps track of total time you spent in the loop. If elapsed time is greater then your timeout value (90000), then you quit the loop. If inside the loop you receive data, you can then quit the loop using break.
– Hichem BOUSSETTA
Mar 8 at 18:51
|
show 4 more comments
I am using a method to instantiate a new Handler object and kicking off a method to do some task in the allocated time. The problem is, this timer is not restarted for each method call and any lingering time is continued with subsequent calls.
I am wondering how to fix this. This is my method:
private void doTask()
viewModel.getFeedBackMessage().set("Calculating ...");
viewModel.fetchData();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
if (viewModel.getData() == null)
viewModel.getFeedbackMessage().set("Failure.");
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
else
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
, 90000);
Edit:
new Thread(() ->
int elapsedTime = 0;
while (elapsedTime < 90000)
try
Thread.sleep(1000);
catch (Exception ex)
if (currentLocation != null)
break;
elapsedTime += 1000;
).start();
java android multithreading
I am using a method to instantiate a new Handler object and kicking off a method to do some task in the allocated time. The problem is, this timer is not restarted for each method call and any lingering time is continued with subsequent calls.
I am wondering how to fix this. This is my method:
private void doTask()
viewModel.getFeedBackMessage().set("Calculating ...");
viewModel.fetchData();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
if (viewModel.getData() == null)
viewModel.getFeedbackMessage().set("Failure.");
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
else
handler.removeCallbacks(this);
handler.postDelayed(this, 90000);
, 90000);
Edit:
new Thread(() ->
int elapsedTime = 0;
while (elapsedTime < 90000)
try
Thread.sleep(1000);
catch (Exception ex)
if (currentLocation != null)
break;
elapsedTime += 1000;
).start();
java android multithreading
java android multithreading
edited Mar 11 at 12:41
Darnold14
asked Mar 8 at 17:37
Darnold14Darnold14
196
196
1
Can you explain more? Why are you call remove callback and post delayed in both cases if/else. Removed is meant to cancel a future task or that future task is already being executed. So, the remove callback call is useless in my opinion.
– Hichem BOUSSETTA
Mar 8 at 18:06
Basically, if the data was found before the allocated time, I want to stop the Handler and if it doesn't find the data, then I still want to stop the Handler
– Darnold14
Mar 8 at 18:09
What you are posting is a delayed task that will be executed once instead you stop it before. But you can not stop the task inside the task itself. The remove callback has to be outside the delayed task, unless I am missing something.
– Hichem BOUSSETTA
Mar 8 at 18:19
How can I execute this timer task every method call?
– Darnold14
Mar 8 at 18:22
1
Okay I see. You are then using a wrong approach. In this type of scenario, you can use a do/while loop in which you check at each iteration if you have data available (this check must be short and non blocking), and then sleep the thread for some interval (let's say one second), after sleep you increment an elapsed integer variable that keeps track of total time you spent in the loop. If elapsed time is greater then your timeout value (90000), then you quit the loop. If inside the loop you receive data, you can then quit the loop using break.
– Hichem BOUSSETTA
Mar 8 at 18:51
|
show 4 more comments
1
Can you explain more? Why are you call remove callback and post delayed in both cases if/else. Removed is meant to cancel a future task or that future task is already being executed. So, the remove callback call is useless in my opinion.
– Hichem BOUSSETTA
Mar 8 at 18:06
Basically, if the data was found before the allocated time, I want to stop the Handler and if it doesn't find the data, then I still want to stop the Handler
– Darnold14
Mar 8 at 18:09
What you are posting is a delayed task that will be executed once instead you stop it before. But you can not stop the task inside the task itself. The remove callback has to be outside the delayed task, unless I am missing something.
– Hichem BOUSSETTA
Mar 8 at 18:19
How can I execute this timer task every method call?
– Darnold14
Mar 8 at 18:22
1
Okay I see. You are then using a wrong approach. In this type of scenario, you can use a do/while loop in which you check at each iteration if you have data available (this check must be short and non blocking), and then sleep the thread for some interval (let's say one second), after sleep you increment an elapsed integer variable that keeps track of total time you spent in the loop. If elapsed time is greater then your timeout value (90000), then you quit the loop. If inside the loop you receive data, you can then quit the loop using break.
– Hichem BOUSSETTA
Mar 8 at 18:51
1
1
Can you explain more? Why are you call remove callback and post delayed in both cases if/else. Removed is meant to cancel a future task or that future task is already being executed. So, the remove callback call is useless in my opinion.
– Hichem BOUSSETTA
Mar 8 at 18:06
Can you explain more? Why are you call remove callback and post delayed in both cases if/else. Removed is meant to cancel a future task or that future task is already being executed. So, the remove callback call is useless in my opinion.
– Hichem BOUSSETTA
Mar 8 at 18:06
Basically, if the data was found before the allocated time, I want to stop the Handler and if it doesn't find the data, then I still want to stop the Handler
– Darnold14
Mar 8 at 18:09
Basically, if the data was found before the allocated time, I want to stop the Handler and if it doesn't find the data, then I still want to stop the Handler
– Darnold14
Mar 8 at 18:09
What you are posting is a delayed task that will be executed once instead you stop it before. But you can not stop the task inside the task itself. The remove callback has to be outside the delayed task, unless I am missing something.
– Hichem BOUSSETTA
Mar 8 at 18:19
What you are posting is a delayed task that will be executed once instead you stop it before. But you can not stop the task inside the task itself. The remove callback has to be outside the delayed task, unless I am missing something.
– Hichem BOUSSETTA
Mar 8 at 18:19
How can I execute this timer task every method call?
– Darnold14
Mar 8 at 18:22
How can I execute this timer task every method call?
– Darnold14
Mar 8 at 18:22
1
1
Okay I see. You are then using a wrong approach. In this type of scenario, you can use a do/while loop in which you check at each iteration if you have data available (this check must be short and non blocking), and then sleep the thread for some interval (let's say one second), after sleep you increment an elapsed integer variable that keeps track of total time you spent in the loop. If elapsed time is greater then your timeout value (90000), then you quit the loop. If inside the loop you receive data, you can then quit the loop using break.
– Hichem BOUSSETTA
Mar 8 at 18:51
Okay I see. You are then using a wrong approach. In this type of scenario, you can use a do/while loop in which you check at each iteration if you have data available (this check must be short and non blocking), and then sleep the thread for some interval (let's say one second), after sleep you increment an elapsed integer variable that keeps track of total time you spent in the loop. If elapsed time is greater then your timeout value (90000), then you quit the loop. If inside the loop you receive data, you can then quit the loop using break.
– Hichem BOUSSETTA
Mar 8 at 18:51
|
show 4 more comments
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068290%2fhow-to-restart-handler-runnable-on-method-call%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068290%2fhow-to-restart-handler-runnable-on-method-call%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you explain more? Why are you call remove callback and post delayed in both cases if/else. Removed is meant to cancel a future task or that future task is already being executed. So, the remove callback call is useless in my opinion.
– Hichem BOUSSETTA
Mar 8 at 18:06
Basically, if the data was found before the allocated time, I want to stop the Handler and if it doesn't find the data, then I still want to stop the Handler
– Darnold14
Mar 8 at 18:09
What you are posting is a delayed task that will be executed once instead you stop it before. But you can not stop the task inside the task itself. The remove callback has to be outside the delayed task, unless I am missing something.
– Hichem BOUSSETTA
Mar 8 at 18:19
How can I execute this timer task every method call?
– Darnold14
Mar 8 at 18:22
1
Okay I see. You are then using a wrong approach. In this type of scenario, you can use a do/while loop in which you check at each iteration if you have data available (this check must be short and non blocking), and then sleep the thread for some interval (let's say one second), after sleep you increment an elapsed integer variable that keeps track of total time you spent in the loop. If elapsed time is greater then your timeout value (90000), then you quit the loop. If inside the loop you receive data, you can then quit the loop using break.
– Hichem BOUSSETTA
Mar 8 at 18:51