//主页面继承基类 基类可有可无
public class MainActivity extends BaseActivity {
private TextView time;
private int ji = 5; //定义倒计时 秒数
//handler异步加载
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
ji--;
time.setText(ji+"s");
if(ji==0){
handler.removeCallbacksAndMessages(null);
//跳转
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
finish();
}else{
handler.sendEmptyMessageDelayed(0,1000);
}
}
};
private SharedPreferences sp;
private SharedPreferences.Editor edit;
@Override
protected int ResultViewId() {
return R.layout.activity_main;
}
@Override
protected void initView() {
//初始化控件
time = findViewById(R.id.time);
}
@Override
protected void initData() {
//SharedPreferences判断是否运行过一次,运行过 直接跳转,没有 继续handler
sp = getSharedPreferences("login", Context.MODE_PRIVATE);
edit = sp.edit();
if(sp.getBoolean("第一次",false)){
//跳转
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
finish();
return;
}
edit.putBoolean("第一次",true);
edit.commit();
//倒计时的方法
getTime();
}
private void getTime() {
handler.sendEmptyMessageDelayed(0,1000);
}
}
启动页面倒计,Handler处理异步加载
最新推荐文章于 2023-01-04 17:31:36 发布