Andengine - handler.postDelayed doesn't work - java

I want to do a simple action in my game after one second. I have my GameScene class which extends Andengine's Scene.
public class GameScene extends Scene{
//(...)
Handler delayHandler;
public GameScene(){
Looper.prepare();
delayHandler = new Handler();
}
//(...)
public void sphereTouched(){
//(...)
delayHandler.postDelayed(new Runnable() {
public void run(){
Log.d("DEB","postDelayed test");
}
}, 1000);
}
}
When sphereTouched function is called operation from postDelayed doesn't run. Others operations from that function work properly. Have I missed something?

use this code for handler.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// your code
}
}, 1000);

Related

postDelayed inside while loop doesn't work?

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?

Why android run PerformClick as not new Thread

In java and android world, to run now Thread we execute it like this
Thread tr = new Thread(new Runnable() {
#Override
public void run() {
todo();
}
});
tr.start();
so tr is new Thread, but in android api PerformClick implements Runnable interface and is called normally by execute run method
private static void handleCallback(Message message) {
message.callback.run();
}
callback is PerformClick
private final class PerformClick implements Runnable {
#Override
public void run() {
performClick();
}
}
I am trying understand why for this case it is not new Thread
Thanks

How to change android timer period?

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

Using handler/runnable to delay the resubmission of a method

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

Java Timer in android

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

Categories