Handler.postDelayed(...)不会延迟Runnable

Kaz*_*oka 3 android runnable postdelayed android-mediaplayer android-handler

我自己实现了一个OnCompletionListener,如下所示:

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); 


    }
}
Run Code Online (Sandbox Code Playgroud)

我想让它暂停一下stopTime秒,但它实际上做的是一旦audiofile结束它跳转到下一个视图.你能指点我走向错误的地方,或者我如何能够以不同的方式延迟切换到另一个活动?

每个提示都表示赞赏!

Phi*_*oda 8

我知道为什么你的处理程序不会延迟发布.

这是因为你的stopTime是0.

您需要为"stopTime" 设置一个值,否则它将为0.例如,将Runnable延迟一秒:

stopTime = 1000; // milliseconds
Run Code Online (Sandbox Code Playgroud)

或者使用您的构造函数:

public SongEndCompletionListener(Context activity, String nextView, int time) {
    this.nextView = nextView;
    actualActivity = activity;
    this.stopTime = time; // you forgot this
}
Run Code Online (Sandbox Code Playgroud)