Android启动画面计时器无法正常工作

时间:2014-01-31 08:07:54

标签: android android-layout timer splash-screen

在我的应用程序中,我将启动画面计时器设置为5秒,之后认为5秒太长,所以我将其更改为1秒,并且我的启动画面在屏幕上看不到并让我等待更多超过5秒我找不到有什么问题所以这是我的Splashscreen代码

    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秒,它会再次显示在屏幕上。

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:1)

您也可以使用Handler

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

答案 2 :(得分:0)

使用此代替计时器

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

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

            }
        }, 5000);