An_handler+Timer倒计时

An_handler+Timer倒计时_第1张图片
An_handler+Timer倒计时_第2张图片
An_handler+Timer倒计时_第3张图片
An_handler+Timer倒计时_第4张图片

public class SplashActivity extends AppCompatActivity {

private MyHandler myHandler = new MyHandler();

private TextView tv_time;

private MyCountDownTimer mc;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//设置Activity为全屏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

//去标题状态栏

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_splash);

tv_time = (TextView) findViewById(R.id.tv_time);

mc = new MyCountDownTimer(5000, 1000);

mc.start();

/**

* 使用handler的postDelayed延迟5秒执行页面跳转

* (与CountDownTimer的millisInFuture一致)

*/

myHandler.postDelayed(new Runnable() {

@Override

public void run() {

startMainActivity();

}

},5000);

}

//将Handler声明为静态内部类

private static class MyHandler extends Handler {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

}

}

//页面跳转的方法

private void startMainActivity(){

Intent intent = new Intent(this,Main3Activity.class);

startActivity(intent);

finish();//完成跳转后销毁闪屏页(从栈内移除)

}

class MyCountDownTimer extends CountDownTimer {

/**

* @param millisInFuture

*      表示以毫秒为单位 倒计时的总数

*      例如 millisInFuture=1000 表示1秒

* @param countDownInterval

*      表示 间隔 多少微秒 调用一次 onTick 方法

*      例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()

*/

public MyCountDownTimer(long millisInFuture, long countDownInterval) {

super(millisInFuture, countDownInterval);

}

public void onFinish() {

tv_time.setText("正在跳转");

}

public void onTick(long millisUntilFinished) {

tv_time.setText("倒计时(" + millisUntilFinished / 1000 + ")");

Log.i("tag","倒计时"+millisUntilFinished / 1000);

}

}

@Override

protected void onDestroy() {

super.onDestroy();

//闪屏页销毁时将消息对象从消息队列移除并结束倒计时

myHandler.removeCallbacksAndMessages(null);

mc.cancel();

Log.i("tag","destory");

}

}

你可能感兴趣的:(An_handler+Timer倒计时)