三秒跳转到另一个页面

时间:2021-12-12 08:55:11
public class MainActivity extends AppCompatActivity {
    private int count=3;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (count==0){
                Intent intent=new Intent(MainActivity.this,startActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        }
    };

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

    private void initView() {
        Timer timer=new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (count>0){
                    count--;
                    handler.sendEmptyMessage(0);
                }
            }
        },0,1000);
    }
}