Timer timer; TimerTask timerTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); initialize(); } private void initialize() { timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { //Start your activity here } }; timer.schedule(timerTask,2500); } @Override public void onBackPressed() { super.onBackPressed(); timer.cancel(); }
或者您也可以使用Handler
Handler handler; Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); initialize(); } private void initialize() { handler = new Handler(); runnable = new Runnable() { @Override public void run() { //start your activity here } }; handler.postDelayed(runnable, 5000); } @Override public void onBackPressed() { super.onBackPressed(); handler.removeCallbacks(runnable); }