Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler.postDelayed(...) does not delay the Runnable

i've implemented an OnCompletionListener by myself which looks like this:

public class SongEndCompletionListener  implements OnCompletionListener{

    String nextView;
    Context actualActivity;
    int stopTime;

    public SongEndCompletionListener(Context activity, String nextView, int time) {
        this.nextView = nextView;
        actualActivity = activity;
    }
    @Override
    public void onCompletion(MediaPlayer arg0) {


            Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() { 
                 public void run() { 
                        try {
                     Intent stopplay;
                     stopplay = new Intent(actualActivity,Class.forName(nextView));
                     actualActivity.startActivity(stopplay);    
                 } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    } 
                 } 
            }, stopTime); 


    }
}

I want it to pause for stopTime seconds, but what it actually does is as soon as the audiofile ends it jumps to the next view. Could you maybe point me towards where i am wrong or how i could delay the switch to another Activity differently?

Every hint is appreciated!

like image 854
Flo Avatar asked Aug 03 '26 20:08

Flo


1 Answers

I know why your Handler does not post delayed.

It is because your stopTime is 0.

You need to set a value for "stopTime", otherwise it will be 0. For example to delay the Runnable for one second:

stopTime = 1000; // milliseconds

Or use your Constructor:

public SongEndCompletionListener(Context activity, String nextView, int time) {
    this.nextView = nextView;
    actualActivity = activity;
    this.stopTime = time; // you forgot this
}
like image 110
Philipp Jahoda Avatar answered Aug 06 '26 11:08

Philipp Jahoda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!