2013-04-27 106 views
1

因此,有一个5秒的启动画面,但底部还有一个skip按钮。这是飞溅如何运行。如何在activity处于onPause时停止线程?

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按钮后发生的情况是:从创建Activity开始经过5秒钟,它将打开MenuActivity,无论我是否已经在那里或其他地方。无论如何要阻止这个?

+1

会发生什么? – 2013-04-27 02:09:56

+0

我不知道如何正确实现,我是Java新手。当我尝试将其置于交换方法中时,此时出现错误。加入的是什么? – Alex 2013-04-27 02:56:07

+0

@ Axtn95连接将使一个线程等待,直到其他完成。这里是一个例子:http://pastebin.com/pfawvt0W – Eugene 2013-04-27 05:38:17

回答

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(); 
} 
+0

我可以证实这个工作!我会研究代码并记住你使用的方法。谢谢! – Alex 2013-04-27 21:55:25

-1

使用:

splashtimer.stop(); 

,你应该设置线程是一个全局对象。

+0

嗯我得到的不赞成,因为以这种方式停止线程是不安全的,可以让你的应用程序和虚拟机处于不可预知的状态。我应该压制它吗? – Alex 2013-04-27 02:50:12

+0

@ Axtn95不!切勿使用停止方法。从不,它由于某种原因而被弃用。 – Eugene 2013-04-27 05:22:28

+0

好的:)我不担心。 – Alex 2013-04-27 06:13:37

0

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

+0

从现在的代码中可以看出,当他睡眠5秒时,这并不会起到帮助,一旦他开始睡觉,将标志设置为真不会有帮助,直到睡眠结束。 – Eugene 2013-04-27 06:22:22

1

你有没有尝试使用一个处理程序

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

调用switchactivity()在可运行的run方法。如果你尝试`splashtimer.join()` 在的onClick()清除所有的回调是可运行

h.removeCallbacks(runnable);