Android - Running a Method Periodically Using Postdelayed() Call

Android - running a method periodically using postDelayed() call

Why don't you create service and put logic in onCreate(). In this case even if you press back button service will keep on executing. and once you enter into application it will not call
onCreate() again. Rather it will call onStart()

How to call a method after a delay in Android

Kotlin

Handler(Looper.getMainLooper()).postDelayed({
//Do something after 100ms
}, 100)

Java

final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
}
}, 100);

Android - running a method periodically using Handler and postDelayed

You are using the wrong Handler! Look at the top of your Java file, you are going to find something like this:

import java.util.logging.Handler;

Replace it with this:

import android.os.Handler;

The Handler from the package android.os is the one you want. The other one from java.util.logging is something completely different and as you already noticed is also an abstract class.

I hope I could help you and if you have any further questions please feel free to ask!

Run a method periodically using ktx handler extensions

This is a part of the core.ktx package

you need to make sure it is included in your gradle file

implementation "androidx.core:core-ktx:1.1.0"

or more relevant/recent version

Once you do, you can convert:

handler.postDelayed(object : Runnable {
override fun run() {
doSomething()
}
}, 1000)

to

handler.postDelayed(delayInMillis = 200L) {
doSomething()
}

to run this more than once just:

while true {
handler.postDelayed(delayInMillis = 200L) {
doSomething()
}
}

Running a task at a specific time using postDelayed

First of all, your delay calculation is correct, but the "pm" detection doesn't work with other languages. It is much better to use the calendar to get the delay:

Calendar calendar = Calendar.getInstance();
long currentTimestamp = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, desiredHour);
calendar.set(Calendar.MINUTE, desiredMinute);
calendar.set(Calendar.SECOND, 0);
long diffTimestamp = calendar.getTimeInMillis() - currentTimestamp;
long myDelay = (diffTimestamp < 0 ? 0 : diffTimestamp);

Now you have the delay in milli secs and can start the handler:

new Handler().postDelayed(mLaunchTask, myDelay);

In my test the following runnable logs at the desired time with no delay.

private Runnable mLaunchTask = new Runnable() {
public void run() {
Calendar calendar = Calendar.getInstance();
Log.d("test", "started at "
+ calendar.get(Calendar.HOUR_OF_DAY) + " "
+ calendar.get(Calendar.MINUTE) + " "
+ calendar.get(Calendar.SECOND)
);
}
};

Maybe your started task needs some seconds to be loaded?

Could the AlarmManager be an alternative?

handler, I want to run periodically

Try the below

m_Handler = new Handler();
mRunnable = new Runnable(){
@Override
public void run() {
if(count == 0){
// do something
count = 1;
}
else if (count==1){
// do something
count = 0;
}
m_Handler.postDelayed(mRunnable, 3000);// move this inside the run method
}
};
mRunnable.run(); // missing

Also check this

Android Thread for a timer

Execute function after 5 seconds in Android

You can use the Handler to add some delay.Call the method displayData() as below so that it will be executed after 5 seconds.

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
displayData();
}
}, 5000);

Note : Do not use the threads like Thread.sleep(5000); because it will block your UI and and makes it irresponsive.

Android - Delay method call

Use Handler for it:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//any delayed code
}
}, 5000);

Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. The time-base is in milliseconds, in eg above it is 5000 milliseconds.

The postDelayed takes two parameters:

  • The Runnable that will be executed.
  • The delay (in milliseconds) until the Runnable will be executed.


Related Topics



Leave a reply



Submit