I want to delay the app before updating the messages to make it look realistic.
when I am trying to delay updating one message it works just fine as shown below
if (currentMessage.getMessageStatue() == MESSAGE_RECEIVED) {
handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
updateMessage();
notifyAdapter();
currentMessage++;
}
};
handler.postDelayed(runnable, 3000);
}
But the problem is that when I am trying to loop throw each message and perform the same task, the app keeps delaying and android force it to stop. this is what I am trying to do:
while (currentMessage.getMessageStatue() == MESSAGE_RECEIVED) {
handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
updateMessage();
notifyAdapter();
currentMessage++;
}
};
handler.postDelayed(runnable, 3000);
}
can I overcome this issue and use a postDelayed handler inside a while loop?
Related
I have simple runnable like so
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
someVariable = true;
}
};
I use it with a Handler and delay like this
handler.postDelayed(runnable, 60000);
if this functionality gets spammed and the above is called repeatedly it adds runnable multiple times to the message queue.
if this is the case is there away to check if there is one in the message queue first before adding another?
Hi the way I do is to remove the messages and callbacks after my handler runs once. A simple example of my code would be
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
Log.d("runnable", "handler invoked just once");
handler.removeCallbacksAndMessages(null);
}
};
handler.postDelayed(runnable, 2000);
handler.postDelayed(runnable, 2000); // demo: if this is called multiple times my runnable code wont run
This question already has answers here:
How to run a thread repeatedly after some interval
(3 answers)
Closed 4 years ago.
I am developing an Android App in which I need to run a piece of code every minute, when I say every minute I mean it should be synced with the device's time so every time the device time changes by one minute my code is executed.
So far I tried this but it is not working:
runnable=new Runnable(){
#Override
public void run(){
long now=SystemClock.uptimeMillis();
long next=now+(60000 - now % 60000);
handler.postAtTime(this, next);
}
};
runnable.run();
There are many other ways to do this:
1) Timer.scheduleAtFixedRate()
2) Thread.sleep(interval)
3) Alarm Manager
In your way it will look like this:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
try{
//do your code here
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same runnable to call it at regular interval
handler.postDelayed(this, "*interval");
}
}
};
handler.postDelayed(runnable, "*interval");
you can see this answer for details
You can use this code:
Thread thread = new Thread(new Runnable()
{
int lastMinute;
int currentMinute;
#Override
public void run()
{
lastMinute = currentMinute;
while (true)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
currentMinute = calendar.get(Calendar.MINUTE);
if (currentMinute != lastMinute){
lastMinute = currentMinute;
Log.v("LOG", "your code here");
}
}
}
});
thread.run();
one can use handle and can give input as milliseconds and we can call the code for every minute and should not use any infinite loops while using two different layouts
final Handler handler = new Handler();
Runnable run = new Runnable() {
#Override
public void run() {
handler.postDelayed(this, 60000);
}
};
handler.post(run);
You can use handler if you want to initiate something every X seconds. Handler is good because you don't need extra thread to keep tracking when firing the event. Here is a Code:
private final static int INTERVAL = 1000 * 60 * 1; //1 minutes
Handler mHandler = new Handler();
Runnable mHandlerTask = new Runnable()
{
#Override
public void run() {
doSomething();
mHandler.postDelayed(mHandlerTask, INTERVAL);
}
};
void startRepeatingTask()
{
mHandlerTask.run();
}
void stopRepeatingTask()
{
mHandler.removeCallbacks(mHandlerTask);
}
The modern method
new Handler().postDelayed(() -> {
}, 30000);
I am writing simple game, where some action must accelerating during the process. The question is how to change timer's period?
timer = new Timer();
timerTask = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//
// I need to change timer's period here
//
}
});
}
};
timer.schedule(timerTask, 0, period);
Will be glad to hear any advices.
I assume that you are performing some logic within the run() method of the TimerTask.
I think a simpler way to go about this would be to use a Handler. This is possibly more idiomatic for Android:
private final Handler mHandler = new Handler();
private final Runnable mTask = new Runnable() {
#Override
public void run() {
// Do your logic.
// Now post again.
mHandler.postDelayed(mTask, /* choose a new delay period */);
}
};
public void init() {
delay = 1000L; // 1 second.
mHandler.postDelayed(mTask, delay);
}
I want to run the getVehicles() method every 10 seconds, I have the following code:
Handler vehiclehandler = new Handler();
final Runnable vehiclerunnable = new Runnable() {
public void run() {
getVehicles(null);
vehiclehandler.postDelayed(this, 10000);
}
};
Yet at the moment it does nothing, I've searched around and can't figure it out.
I'm new to android and have never used a handler before, only a runnable to tell something to 'runOnUiThread'.
did you run
vehiclehandler.post(vehiclerunnable)
at least once?
I mean outside the Runnable
final Handler lHandler = new Handler();
Runnable lRunnable = new Runnable() {
#Override
public void run() {
// do stuff
lHandler.postDelayed(this, 10000);
}
};
lHandler.post(lRunnable);
Here is an adjustment to your code that will make it run properly
Handler vehiclehandler = new Handler();
vehiclehandler.postDelayed(new Runnable(){
public void run(){
getVehicles(null);
}
},10000);
But this will just delay your code before get executed. If you want to repeat the process over and over again you have to use Timer, something like:
private static Timer timer = new Timer();
timer.scheduleAtFixedRate(new mainTask(), 0, 10000);
private class mainTask extends TimerTask
{
public void run()
{
getVehicles(null);
}
}
How can this be done in android?
public final Timer timer = new Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do task here
}
});
I need to be able to call timer.start(); from the Activity that timer is in.
In most cases it is much better to use a Handler instead of Timer. Handler is capable of sending delayed messages. Benefits of using Handler are:
it runs on the main (UI) thread -> can access Views (unlike the
Timer, which cannot dircetly access Views)
You can remove pending delayed messages if you want
Less code
Example:
class MyActivity extends Activity {
private static final int DISPLAY_DATA = 1;
// this handler will receive a delayed message
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// Do task here
if (msg.what == DISPLAY_DATA) displayData();
}
};
#Override
void onCreate(Bundle b) {
//this will post a message to the mHandler, which mHandler will get
//after 5 seconds
mHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 5000);
}
}
Android has a nice CountDownTimer class, too
Try something like this:
// this will run when timer elapses
TimerTask myTimerTask = new TimerTask() {
#Override
public void run() {
// ...
}
};
// new timer
Timer timer = new Timer();
// schedule timer
timer.schedule(myTimerTask, delayInMs);
If you want task to be done in activity's main thread, modify it like this:
// get a handler (call from main thread)
final Handler handler = new Handler();
// this will run when timer elapses
TimerTask myTimerTask = new TimerTask() {
#Override
public void run() {
// post a runnable to the handler
handler.post(new Runnable() {
#Override
public void run() {
// ...
}
});
}
};
// new timer
Timer timer = new Timer();
// schedule timer
timer.schedule(myTimerTask, delayInMs);
I have answered this in another question.
I would avoid the TimerTask, if you fire a lot of them off, they are not only difficult to manage but bad for performance.
I would recommend a Handler for pretty much any Time based task these days.
See Timer application