java run task later code example
Example 1: java do something after x seconds without stopping everything else
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
}
},
5000
);
Example 2: call function after specific time java android
Runnable mToastRunnable;
Handler mHandler = new Handler();
mToastRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "This is a delayed toast", Toast.LENGTH_SHORT).show();
mHandler.postDelayed(this, 3000000);
getlocation();
}
};
mToastRunnable.run();
mHandler.removeCallbacks(mToastRunnable);