1. 程式人生 > >倒計時-----第二次直接進入

倒計時-----第二次直接進入

activity_main

<TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/time"
        android:gravity="center"
        android:text="s"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/a1" />

MainActivity

package com.example.wanghuiqi_20180921_moni;

import java.io.ObjectOutputStream.PutField;

import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.animation.AlphaAnimation; import android.view.animation.AnimationSet; import android.view.animation.Transformation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.TextView;

//第一個Activity   倒計時跳轉頁面
public class MainActivity extends Activity {

	private TextView time;
	private ImageView image;
	private MyHandler handler = new MyHandler();
	private int sum = 0;
	private SharedPreferences pref;

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

		time = (TextView) findViewById(R.id.time);
		image = (ImageView) findViewById(R.id.image);

		// 倒計時
		pref = getSharedPreferences("person", MODE_APPEND);
		handler.sendEmptyMessageDelayed(sum, 1000);
		// 第二次直接進入
		if (pref.getInt("TIME", 1) == 0) {
			startActivity(new Intent(MainActivity.this, TwoActivity.class));
			finish();
			return;
		}
	}

	int start_time = 3;

	public class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			start_time--;
			time.setText(start_time + "s");
			if (start_time == 0) {
				Editor editor = pref.edit();
				editor.putInt("TIME", 0);
				editor.commit();
				startActivity(new Intent(MainActivity.this, TwoActivity.class));
				finish();
			} else {
				handler.sendEmptyMessageDelayed(sum, 1000);
			}
		}
	}

	// 銷燬
	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler.removeMessages(sum);
	}
}