I wonder what the best way is to implement this behavior:
I have an event X with an id that happens from time to time.
If the event with a certain id happened, I want to execute some code after 25 seconds, except if X with the same id happens again, in that case I want it to be postponed again with 25 seconds.
What is the best way to implement this?
Here is how you can use a simple handler to delay running something for 25 seconds. It's up to you to figure out how to filter on some ID.
Handler handler = new Handler();
public void myEvent() {
// Remove any callback that may be registered, this will reset it if called before
handler.removeCallbacks(runMe);
// Execute the runnable in 25 seconds
handler.postDelayed(runMe, 25000);
}
Runnable runMe = new Runnable() {
#Override
public void run() {
// This will run after the postDelayed expires in 25 seconds
Log.d("TAG", "Run me at some time");
}
};
Related
I've a TextView where I want to set a message according to time ( hour of day). To achieve this I'm using-
Thread t2 = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(3600000);
handler.post(new Runnable() {
#Override
public void run() {
int hour=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
update(hour);
}
});
}
} catch (InterruptedException e) {
}
}
};
t2.start();
where update(hour) is used to update the message.
This works fine, however, the updating is done depending on the launching time. For example, the message should update at time 20:00. But if the app is launched at 19:59, the updating takes place at 20:59.
If I use Thread.sleep(1000) it works just as expected. But I feel like wasting resource by running the thread every second just to look for a 1 hour event. Is there any better way to do this?
Surely you need to check the current time, get the minutes past the hour, then work out from there when the next hour 00 will come. Then, just sleep time for those minutes, then for all the hours after that sleep for the full hour.
Take a look at AlarmManager.
The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.
I am trying to calculate the length of time that has elapsed since a button was released. I understand how to calculate this by waiting for the user to interact with the application again but i would like to be able to wait a fixed period - say 2 seconds - before a method is called or another event is triggered.
I am not sure how i can achieve this - is there a way of doing this without waiting for the user to press the button again?
Hey perfect_comment for this kind of timed task there is a few things you could do one of which is using ScheduledExecutorService this will allow you to set up a runnable to do some task at a fixed interval. In your onCreate method you would declare the shceduler like this
scheduler = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> future;
then tell your your scheduler to run some future task like this
`future = scheduler.scheduleWithFixedDelay(mRunnable, 1, DISCOVERY_INTERVAL, TimeUnit.SECONDS);
where runnable is a Runnable you have defined
mRunnable = new Runnable() {
#Override
public void run() { //printer has been found execute runnable
//your task code
}
};
I have been using 5 Handler tasks in my application with different post delays but all of them are working at a different speed irrespective of the delay time that I have specified, is there a better way to get exact time delay in between of multiple tasks,
Sample Code:
H2 = new Handler();
R2 = new Runnable()
{ #Override
public void run()
{ H2.postDelayed(R2, 100);
//Do something
}
};
H2.postDelayed(R2, 0);
Task would run in variable time intervals sometimes way more variable.
Okay so I have a rather weird problem here:
I have one method calling a sequence of delayed methods, for example:
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flash(true, 3000);
flash(false, 1200);
}
});
And the flash function goes:
private void flash(final boolean color, int duration) {
// SLEEP duration MILLISECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
changeColour(color);
}
}, duration);
}
What happens is that the 1200 ms timer kicks off before the 3000 ms timer although the 3 second one should go first. I tried adding a trigger for the timers but that only freezes the whole app:
...
public void onClick(View view) {
flash(true, 3000);
while(wait); //stop here until the timer triggers wait to false
wait = false;
flash(false, 1200);
}
...
public void run() {
changeColour(color);
wait=false;
}
...
Any help is appreciated, thank you.
What happens is that the 1200 ms timer kicks off before the 3000 ms
timer although the 3 second one should go first.
The first should trigger first only if you set the exact time interval on both of them. However, right now you just trigger two flashes, one to be run after 3 seconds and one after 1,5 seconds(from the current time).
In order to do what you want you need to take in consideration the time of the first trigger when posting the second message:
flash(true, 3000); // post a message after 3 seconds
flash(false, 4200); // post a message after 4,2 seconds(so at 1,2 seconds after the first flash runs)
I want to run some code for 20 seconds precisely. It is similar to a loop but instead of having a variable I have time (in seconds).
I should have a time condition like this:
do
{ variable++ }
while (sec < 20)
How it is possible to do this in Android??
My application should run this 20 sec code after the user presses a button.
You can use the Handler class in Android on a runnable and then use the postDelayed() method. That way you will be able to update the UI during that 20 seconds on the progress of the thread. A good example of this is hear. Your code might look something like this ...
Handler handler = new Handler();
final Runnable r = new Runnable(){
public void run() {
//Do thing after 20 sec
}
};
handler.postDelayed(r, 20000);