App进入首页三秒后跳转到主页面,第二次进入直接跳到主页面,带倒计时

时间:2022-06-05 22:06:47
如果需要首页面倒计时跳转 直接将下面代码复制到工程中,
package com.example.a1.zhaoshan20170619;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
//首页实现三秒跳转,第二次进入直接跳过首页面 public class MainActivity extends AppCompatActivity {
    private TextView timerView;
    private int time = 3;
    private Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                  //time--;  //timerView.setText("还有"+ time+"秒跳转");  Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                    startActivity(intent);
                    break;

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

        //实例化一个sp对象  SharedPreferences sp = getSharedPreferences("USER",MODE_PRIVATE);
        //得到一个编辑器  SharedPreferences.Editor edit = sp.edit();
        //得到一个bool boolean first = sp.getBoolean("first", true);
        if (first){
            //如果是第一次就给他put一个值 改变状态为false  edit.putBoolean("first",false);
            edit.commit();
            initview();//初始化数据  handler.sendEmptyMessageDelayed(0,3000);
        }else {
            //直接跳转  Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            startActivity(intent);
        }
    }

    private void initview() {

        timerView = (TextView) findViewById(R.id.textView);

        //实例化一个MyCount MyCount myCount = new MyCount(3000,1000);
        // 开始倒计时:  myCount.start();

    }
  //创建一个类继承CountDownTimer  /**  * Rewrite 'CountDownTimer' method.  *  * @param millisInFuture  * 倒计时总数,单位为毫秒。  * @param countDownInterval  * 每隔多久调用onTick一次  * @author DaiZhenWei  *  */  public class MyCount extends CountDownTimer {


        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            timerView.setText("");

        }
        @Override
        public void onTick(long millisUntilFinished) {
            timerView.setText("请等待60(" + millisUntilFinished / 1000 + ")...");
        }

    }
}