Runnable run = new Runnable() {
#Override
public void run() {
Log.i("runnable has run","a second must have passed");
}
};
handler.post(run);
When I write this code, this error appears:
cannot resolve`postDelayed.
Please tell me how to remove it.
I think you want to put delay use this code.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// do something here
}
}, 1000);
Related
this is a simple code to understand the runnable .I tried but not working . can you guys help me pls this is my code
public class Autostart extends activity implements Runnable {
#override
public void run (){
System.out.println ("message");
}
}
}
this not printing any statements
If you are using an Activity, you need to write your code inside Activity lifecycle methods. onCreate() is called when the Activity is created. So starting your Runnable here would be the correct way to do it.
#Override
public void onCreate(Bundle savedInstanceState) {
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
System.out.println ("message");
}
};
handler.postDelayed(r, 1000);
}
You have to create a Thread object and call start() using that object.
Thread t = new Thread(this);
t.start();
Or Just use Handler
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do Something here
}
}, 5000);
You can use below code to print a value after regular interval of time
public void callAsynchronousTask() {
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
Log.e("on print timee", your value);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000); // will execute after 1 sec
}
Hope this will help you
I found a similar solution to Swayam (android implements runnable not working?), however another handler.postDelayed reference within run() was required;
public void onCreate(
...
final Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
Log.i(TAG, "message");
handler.postDelayed(this, 1000);
...
}
};
handler.postDelayed(r, 1000);
Try following code
Handler mainThreadhandler = new Handler(getMainLooper());
mainThreadhandler.post(new Runnable(){
public void run(){
// UI work
}
});
public class Autostart extends activity implements Runnable {
Thread = thread;
#override
public void onCreate() {
thread = new Thread(this);
thread.start();
}
#override
public void run (){
System.out.println ("message");
}
}
I am using postDelayed with TextView to hide it after some time. Now, I want to remove postDelayed if user click on button.
My code is as below :
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setText("");
}
}, 10000);
How to do this ?
Create your thread in separate place below...
private Runnable mTimerExecutor = new Runnable() {
#Override
public void run() {
tvRQPoint.setText("");
}
};
Then call it as follows to execute....
tvRQPoint.postDelayed(mTimerExecutor, 10000);
When you want to cancel the postDelay execution then cancel as follows...
tvRQPoint.removeCallbacks(mTimerExecutor);
check this
Runnable runnable = new Runnable() {
public void run() {
tvRQPoint.setText("");
}
};
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(runnable, 10000);
to remove it
tvRQPoint.removeCallbacks(runnable);
boolean clicked=false;
onClick event
clicked=true;
and in postDelayed
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
if(!clicked)
tvRQPoint.setText("");
}
}, 10000);
Use below code inside onClick. It will remove.
private final Runnable r = new Runnable() {
public void run() {
tvRQPoint.setText("");
Handler handler = new Handler();
handler.postDelayed(this, 2000);
}
}
};
and then use this inside onClick of button
handler.removeCallbacks(r);
For more information check this link
I would like to know if it's possible to use handler().postdelayed twice?
I mean, I want to create a button, that when clicked it change the color and stay in this state 1 second, then, after 1 second another button change the color.
I've created the following code:
In the onclicklistener:
btn3.setBackgroundColor(Color.WHITE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
checkAnswer();
waitAnswer();
btnRsp3.setBackgroundResource(R.drawable.selector);
}
}, 1000);
CheckAnswer:
public void CheckAnswer(){
btn1.setBackgroundColor(Color.GREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
}
}, 500);
btn1.setBackgroundResource(R.drawable.selector);
}
I think the problem is on CheckAnswer because it seems it doesn't stop in this postDelayed and step to the waitAnswer.
Thanks
Why do you expect it to stop on postDelayed? postDelayed places your Runnable to the Handler Looper queue and returns. Since both handlers are created on the same looper, the second runnable is executed after the first one terminates (plus whatever left of the 500 ms delay)
UPDATE:
You need something like that
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 1000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundResource(R.drawable.selector);
}
}, 2000);
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
//Your Work
}
}, 1000);
I want to change some values onBackPressed method... And i override it like this:
#Override
public void onBackPressed() {
final Handler backHandler = new Handler();
backHandler.postDelayed(new Runnable() {
public void run() {
exitCount = 0;
Log.d("exitCount", "exitCount: " + exitCount);
}
}, Toast.LENGTH_SHORT);
}
But the problem is handler posts immediately... There's no delay. Where am i doing wrong?
Sorry if this is a lame question, i'm pretty new on Android. Thanks in advance.
That is because Toast.LENGTH_SHORT value is zero. Try declaring your constant with a delay value you choose. see here
Make the handler part of an activity (or part of a thread you are posting a message to if its not for the UI thread), and use a millisecond delay rather than Toast.LENGTH_SHORT which has a value of zero so it will happen instantly.
public class SomeActivity extends Activity {
private Handler mHandler = new Handler();
#Override
public void onBackPressed() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Log.d("tag", "Hello, Handler!");
}
}, 1000); // one second
}
}
Use belo code I hope it will work.
runOnUiThread(new Runnable() {
#Override
public void run() {
backHandler.postDelayed(new Runnable() {
public void run() {
exitCount = 0;
Log.d("exitCount", "exitCount: " + exitCount);
}
}, Toast.LENGTH_SHORT);
}
});
Sorry, I keep on trying to adapt the tokens, but somehow I can't manage this one.
I have the following code:
timer.schedule(new TimerTask(){
runOnUiThread(new Runnable() {
public void run(){
SplashImage.setImageDrawable(aktieknop);}
});
},SplashTime);
}
Like this the code 'works':
timer.schedule(new TimerTask(){
// runOnUiThread(new Runnable() {
public void run(){
SplashImage.setImageDrawable(aktieknop);}
// });
},SplashTime);
}
Can you please help me solving this silly issue? Thanks a lot!
You must call this code line " SplashImage.setImageDrawable(nSplashImage); " from your run method in a runOnUIThread() method like this:
runOnUiThread(new Runnable() {
public void run() {
SplashImage.setImageDrawable(nSplashImage);
}
});
This is because you cannot change UI components on a non UI thread.
For the splash Screen you can use the Handler and send the delayed message.
Handler splashHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//Here you can do what ever you want
}
};
int SPLASHTIME=2000;//your wish
splashHandler.sendMessageDelayed(msg, SPLASHTIME);