I want to write an Android application which when opened call a method after 20 seconds then auto call method every 10 seconds.
Any thoughts on how to proceed?
This example demonstrates how do I run a method every 10 seconds in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.
putBoolean("key",1); //or you can also use editor. putString("key","value"); editor. commit();
You could do it with a single Handler
like this:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 20 seconds
}
}, 20000); //the time is in miliseconds
And to repeate the same every 10 seconds you could add the following line in the same method
handler.postDelayed(this, 10000);
Like this:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 20 seconds
handler.postDelayed(this, 10000);
}
}, 20000); //the time is in miliseconds
Please check the below code.
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Enter your code
}
}, 0, 20000);//put here time 1000 milliseconds=1 second
Hope this code will help you.
CountDownTimer cdt;
cdt = new CountDownTimer(timeint * 60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.i(TAG, millisUntilFinished " millis left");
}
@Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With