利用getsharedprefreence 倒计时只出现一次
public class MainActivity extends AppCompatActivity {
int cont =3;
private TextView textView;
private SharedPreferences.Editor edit;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (cont <=0){
startActivity(new Intent(MainActivity.this,Main2Activity.class));
finish();
return;
}
textView.setText(cont--+"S");
handler.sendEmptyMessageDelayed(100,1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.count);
SharedPreferences user = getSharedPreferences("User", MODE_PRIVATE);
boolean key = user.getBoolean("key", false);
edit = user.edit();
if (key){
startActivity(new Intent(MainActivity.this,LoginActivity.class));
finish();
}else {
handler.sendEmptyMessageDelayed(1,100);
}
edit.putBoolean("key",true);
edit.commit();
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}