import java.util.Timer;import java.util.TimerTask;
class Test {public static void main( String [] args ) {int delay = 5000;// in ms
Timer timer = new Timer();
timer.schedule( new TimerTask(){public void run() {System.out.println("Wait, what..:");}}, delay);
System.out.println("Would it run?");}}
Handler myHandler = new DoSomething();Message m = new Message();m.obj = c;//passing a parameter heremyHandler.sendMessageDelayed(m, 1000);
class DoSomething extends Handler {@Overridepublic void handleMessage(Message msg) {MyObject o = (MyObject) msg.obj;//do something here}}
Handler(Looper.getMainLooper()).postDelayed({//Do something after 100ms}, 100)
Java
final Handler handler = new Handler(Looper.getMainLooper());handler.postDelayed(new Runnable() {@Overridepublic void run() {//Do something after 100ms}}, 100);
final Handler handler = new Handler();Timer t = new Timer();t.schedule(new TimerTask() {public void run() {handler.post(new Runnable() {public void run() {//DO SOME ACTIONS HERE , THIS ACTIONS WILL WILL EXECUTE AFTER 5 SECONDS...}});}}, 5000);
private static long SLEEP_TIME = 2 // for 2 second..MyLauncher launcher = new MyLauncher();launcher.start();..private class MyLauncher extends Thread {@Override/*** Sleep for 2 seconds as you can also change SLEEP_TIME 2 to any.*/public void run() {try {// SleepingThread.sleep(SLEEP_TIME * 1000);} catch (Exception e) {Log.e(TAG, e.getMessage());}//do something you want to do//And your code will be executed after 2 second}}
import android.os.CountDownTimer;
// calls onTick every second, finishes after 3 secondsnew CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {Log.d("log", millisUntilFinished / 1000);}
public void onFinish() {// called after count down is finished}}.start();
如果您使用Android Studio 3.0及更高版本,您可以使用lambda表达式。2秒后调用方法callMyMethod():
new Handler().postDelayed(() -> callMyMethod(), 2000);
如果您需要取消延迟运行,请使用:
Handler handler = new Handler();handler.postDelayed(() -> callMyMethod(), 2000);
// When you need to cancel all your posted runnables just use:handler.removeCallbacksAndMessages(null);
handler.removeMessages(int what);// Remove any pending posts of messages with code 'what' that are in the message queue.
handler.removeCallbacks(Runnable r)// Remove any pending posts of Runnable r that are in the message queue.
override fun onPause() {super.onPause()if(mDelayedJob != null && mDelayedJob!!.isActive) {A35Log.v(mClassTag, "canceling delayed job")mDelayedJob?.cancel() //this should throw CancelationException in coroutine, you can catch and handle appropriately}}
final Handler handler = new Handler(Looper.getMainLooper());handler.postDelayed(new Runnable() {@Overridepublic void run() {//Do something after 100ms}}, 100);
class MainActivity : AppCompatActivity() {
private lateinit var handler: Handler
override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)handler= Handler()handler.postDelayed({doSomething()},2000)}
private fun doSomething() {Toast.makeText(this,"Hi! I am Toast Message",Toast.LENGTH_SHORT).show()}}