How to Use Postdelayed() Correctly in Android Studio

How to use postDelayed() correctly in Android Studio?

You're almost using postDelayed(Runnable, long) correctly, but just not quite. Let's take a look at your Runnable.

final Runnable r = new Runnable() {
public void run() {
handler.postDelayed(this, 1000);
gameOver();
}
};

When we call r.run(); the first thing it's going to do is tell your handler to run the very same Runnable after 1000 milliseconds, and then to call gameOver(). What this will actually result in is your gameOver() method being called twice: once right away, and a second time once the Handler is done waiting 1000 milliseconds.

Instead, you should change your Runnable to this:

final Runnable r = new Runnable() {
public void run() {
gameOver();
}
};

And call it like this:

handler.postDelayed(r, 1000);

Hope this helps.

[android studio]having problem with handler.postdelayed

Since the postDelayed is a method, you have to use () along with it. It takes 2 parameters, first one is a Runnable object and second one is a millisecond value to delay which is an int type. Also Handler() constructor is depracated, that's why you have to bind your handler to a looper object, commonly to the main looper. Here is the fixed code:

// Make sure you import the right Handler class
import android.os.Handler;

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed( () -> {
SharedPreferences preferences = getSharedPreferences("PREFS", 0);
String password = preferences.getString("password","0");
if (password.equals("0")) {
Intent intent = new Intent (getApplicationContext(),CreatePasswordActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent (getApplicationContext(),InputPasswordActivity.class);
startActivity(intent);
finish();
}
}, millisecondDelay);

UPDATE
I've noted that your postdelayed syntax isn't correct. It must be as postDelayed. I updated my answer chek the code out again.

How can I use Intent in postDelayed using Android?

Try this:

int SPLASH_TIME_OUT = 3000;
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
new Handler().postDelayed(() -> {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(i);

// close this activity
finish();
}, SPLASH_TIME_OUT);

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 postDelayed works but can't put it in a loop?

Your main problem is that your loop doesn't take a break, so it's constantly running the function, posting a gazillion runnables.

What you want to do is make the runnable call itself after another 100 ms. Take a look at this example:

if(old_x != new_x)
timedMoveIV(100);

Here you simply call the function once. After that, you let the posted runnable decide whether or not it needs to move again:

public void timedMoveIV(final int time_ms)
{
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run()
{
if(new_x > img.getX())
img.setX(img.getX() + 1);
else
img.setX(img.getX() - 1);

// if not in position, call again
if((int)img.getX() != new_x)
timedMoveIV(time_ms);
}
}, time_ms);
}

It should stop once img.getX() == new_x. Notice the cast to int, though, because if you leave it out, you might get some oscillation as it gets within a pixel of the destination.

This assumes that new_x is an int. If it's a float as well, you should either cast both to int to compare, or compare them to a minimum threshold. For instance, if there's less than 0.5 difference, treat it as "done".



Related Topics



Leave a reply



Submit