计时器java启动活动

时间:2016-06-30 11:10:14

标签: java android

我正在根据教程here实现启动画面,但启动画面几乎立即消失得非常快。包含计时器以仅在例如1秒之后启动新活动的最佳方式是什么。我的启动画面活动文件如下所示:

    public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
} 

该方法依赖于drawable和style资源。

4 个答案:

答案 0 :(得分:1)

最简单的方法是使用Handler

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, DateUtils.SECOND_IN_MILLIS);

答案 1 :(得分:1)

Splash代码: -

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

public class Splash extends AppCompatActivity {
    // Splash screen timer

    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);

        // Showing splash screen with a timer.
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // Start your application main_activity
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);

                // Close this activity
                finish();
            }
        }, SPLASH_TIME_OUT); // Timer
    }
}

答案 2 :(得分:0)

public class SplahActivity extends Activity {

    public static final int Tick = 1000;
    public static final int Complete = 5000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new CountDownTimer(Complete, Tick) {

            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                //start Activity
               Intent intent = new Intent(this, MainActivity.class);
               startActivity(intent);
               finish();
            }
        }.start();
    }

答案 3 :(得分:0)

使用此

timerHandler = new Handler();
getmi_runnable = new Runnable() {
@Override
public void run() {
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    startActivity(intent);
    finish();
  }
};
timerHandler.postDelayed(getmi_runnable, 4000L);