<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="30dp"
android:id="@+id/mtext"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:id="@+id/mimage"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
int time=5;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(time==0){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}else {
mtext.setText(time+"");
}
}
};
private TextView mtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtext = findViewById(R.id.mtext);
SharedPreferences sp = getSharedPreferences("name", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
boolean jump = sp.getBoolean("true",false);
if (jump){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}else{
edit.putBoolean("true",true);
edit.commit();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
time--;
handler.sendEmptyMessage(0);
}
}, 0,1000);
}
}
}