1

所以,有一个 5 秒的闪屏,但skip底部还有一个按钮。这就是飞溅的运行方式。

public class Splash extends Activity implements View.OnClickListener {

MediaPlayer splashsong;
Button skipsplash;

@Override
protected void onCreate(Bundle chiefsplash) {
    // TODO Auto-generated method stub
    super.onCreate(chiefsplash);
    setContentView(R.layout.splash);

    splashsong = MediaPlayer.create(Splash.this, R.raw.jingle);
    splashsong.start();
    skipsplash = (Button) findViewById(R.id.skipsplash);

    skipsplash.setOnClickListener(this);

    Thread splashtimer = new Thread() {
        public void run() {
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                switchactivity();
            }
        }
    };

    splashtimer.start();
}

@Override
public void onClick(View skipbutton) {
    switchactivity();
}

private void switchactivity() {
    Intent aftersplash = new Intent("com.example.testapp.MENU");
    startActivity(aftersplash);
}

@Override
protected void onPause() {

    super.onPause();
    splashsong.release();
    finish();
}

}

现在我希望有一个选择来skip打断 5 秒等待的按钮,我猜不是在线程运行时。所以在我点击skip按钮后发生的事情是:一旦从创建 5 秒过去,无论我已经在那里还是在其他地方Activity,它都会打开。Menu Activity无论如何要阻止这一切?

4

4 回答 4

1

您是否尝试过使用处理程序

          Handler h = new Handler();
          h.postDelayed(runnable, delayMillis);

在runnable的run方法中调用switchactivity()。在 onClick() 中删除该可运行对象的所有回调

h.removeCallbacks(runnable);
于 2013-04-27T17:15:08.560 回答
0

不要使用 5 秒的睡眠间隔,而是在循环中使用小间隔,并使用标志组合在您想要的任何时候优雅地跳过线程。请尝试以下修改后的代码。

public class Splash extends Activity implements View.OnClickListener {

MediaPlayer splashsong;
Button skipsplash;
boolean skipWait = false;

@Override
protected void onCreate(Bundle chiefsplash) {
// TODO Auto-generated method stub
super.onCreate(chiefsplash);
setContentView(R.layout.splash);

splashsong = MediaPlayer.create(Splash.this, R.raw.jingle);
splashsong.start();
skipsplash = (Button) findViewById(R.id.skipsplash);

skipsplash.setOnClickListener(this);

Thread splashtimer = new Thread() {
    int waitTime = 0;
    public void run() {
        while(waitTime < 5000 && !skipWait) {
            try {
                sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
            waitTime += 500;
        }
        switchactivity();
    }
};

splashtimer.start();
}

@Override
public void onClick(View skipbutton) {
    skipWait = true;
}

private void switchactivity() {
Intent aftersplash = new Intent("com.example.testapp.MENU");
startActivity(aftersplash);
}

@Override
protected void onPause() {

super.onPause();
splashsong.release();
finish();
}
于 2013-04-27T16:49:47.263 回答
0

您可以将布尔值 isSplashRunning 设置为 true,在运行中将所有操作都放在 While(isSplashRunning ){} 循环中,并且当您想停止线程时设置 isSplashRunning = false; splashtimer.join();

于 2013-04-27T03:06:27.517 回答
-1

利用:

splashtimer.stop();

并且您应该将线程设置为全局对象。

于 2013-04-27T02:15:45.320 回答