1. 程式人生 > >SharedPreFerences儲存做引導頁第二次不引導直接跳轉

SharedPreFerences儲存做引導頁第二次不引導直接跳轉

一、佈局

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=".MainActivity">

<ImageView
    android:id="@+id/image_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:src="@drawable/bg" />

<TextView
    android:id="@+id/text_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:layout_marginTop="25dp"
    android:layout_marginRight="25dp"
    android:background="@drawable/shape_item"
    android:textSize="20sp"
    android:padding="15dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout> 二、MainActivity public class MainActivity extends BaseActivity {

private ImageView image;
private TextView text;
private int time = 5 ;


@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case 0:
                if(time == 0){
                    //獲取幫助類存資料
                    SharedPreferences.Editor edit = data.edit();
                    //存入資料
                    edit.putBoolean("FLAG",true);
                    //提交資料
                    edit.commit();
                    startActivity(new Intent(MainActivity.this,TwoActivity.class));
                    finish();
                    return;
                }
                time --;
                text.setText(""+time);
                handler.sendEmptyMessageDelayed(0,1000);

                break;
        }
        super.handleMessage(msg);
    }
};
private SharedPreferences data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //找控制元件
    initView();

    //載入資料
    initData();

    //建立動畫玩一玩
    AlphaAnimation aa = new AlphaAnimation(0, 1);
    aa.setDuration(5000);
    image.setAnimation(aa);

    //傳送handler
    handler.sendEmptyMessage(0);

    //點選事件
    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            time = 0 ;
        }
    });
}

//載入佈局
@Override
protected int initLayout() {
    return R.layout.activity_main;
}

//找控制元件
@Override
protected void initView() {
    image = findViewById(R.id.image_main);
    text = findViewById(R.id.text_main);
}

//初始化資料
@Override
protected void initData() {
    //建立SharedPreferences儲存
    data = getSharedPreferences("data", MODE_PRIVATE);
    //獲取布林型別資料
    boolean flag = data.getBoolean("FLAG", false);
    //如果為false直接跳轉
    if(flag){
        startActivity(new Intent(MainActivity.this,TwoActivity.class));
        finish();
    }
}

//銷燬主執行緒
@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacksAndMessages(null);
}

}