2014-01-31 31 views
1

在我的应用程序中,我将启动画面计时器设置为5秒,之后认为5秒太长,因此我将其更改回1秒,并且我的启动画面未显示屏幕,让我等待超过5秒,我找不到什么是错了,所以这里是我的启动画面代码Android的启动画面计时器无法正常工作

public class Splash extends Activity 
{ 
    private Timer_Countdown timer_Countdown = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.splash_screen); 
     timer_Countdown = new Timer_Countdown(5000, 1000); 
     timer_Countdown.start(); 
    } 

    class Timer_Countdown extends CountDownTimer 
    { 
     public Timer_Countdown(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
     } 

     @Override 
     public void onFinish() { 
      timer_Countdown.cancel(); 
      Intent startIntent; 
      startIntent = new Intent("android.intent.action.MAINMENU"); 
      startActivity(startIntent); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 

     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     finish(); 
    } 
    } 

还有最后一两件事,如果我改回5秒就会出现在屏幕上再次。

+0

这是上帝的告诉你,闪屏是邪恶的方式。请重新考虑。 http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ – Simon

回答

3

为什么你正在使用这么多的代码只是使用闪屏。简单一点,你可以使用下面的代码。

public class Splash extends Activity { 

    Timer timer = new Timer(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     timer.schedule(new TimerTask() { 
       public void run() { 
        Intent intent = new Intent(Splash.this, NewActivity.class); 
        startActivity(intent); 
           finish(); 
       } 
      }, 2000); 
    } 
} 
+0

如果你想要1秒钟,用你想要的秒数改变计时器'5000'然后让它像这样'1000' – InnocentKiller

+0

同样的问题,如果我设置的数字少于5,它不会显示在屏幕上 –

+0

只需复制上面的完整代码并用您的代码替换。它会保持你的屏幕2秒钟,然后自动移动到下一个屏幕。 – InnocentKiller

0

使用此而不是定时器

new Handler().postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       //code for starting new activity 

      } 
     }, 5000); 
1

可以使用处理器也

Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

      startActivity(new Intent(SplashActivity.this, YourNewActivity.class)); 
      finish(); 

     } 
    }, 3000); 

或使用定时器与定时器日程

public class Splash extends Activity { 

Timer t= new Timer(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    t.schedule(new TimerTask() { 
      public void run() { 
       Intent n= new Intent(Splash.this, YourNewActivity.class); 
       startActivity(n); 
      } 
     }, 3000); 
} 
}