In Java, there is a way where you set a Handler and a Runnable so you can return from a function and get that Runnable thing to run later.
What is the equivalent of this tasks in C#?
private Runnable mShowContentRunnable = new Runnable() {
#Override
public void run() {
}
};
private void obtainData() {
mainActivity.getActionBar().setHomeButtonEnabled(false);
mHandler = new Handler();
mHandler.postDelayed(mShowContentRunnable, 375);
}
Related
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?
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 can we mult-thread in libgdx ?
I tried:
new Thread(new Runnable() {
#Override
public void run() {
// do something important here, asynchronously to the rendering thread
final int result = 10;
// post a Runnable to the rendering thread that processes the result
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
Array<Integer> results = new Array<Integer>;
Gdx.app.log("Thread1", "Worked");
results.add(result);
}
});
}
}).start();
I have read here and it is official. But it did not tell me specifically how to access the array.
How can I access the result? through postRunnable
You could subclass Runnable and keep the array as a member in that new subclass, there is a nice explanation for that approach here https://stackoverflow.com/a/7762490/4802055
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);
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);
}
}