android保存第一次安裝狀態並初始化數據

package com.example.cuboo.gatherhar.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.litepal.crud.DataSupport;

public class LauchActivity extends AppCompatActivity {
    private float nowVersionCode,saveVersionCode;
    private static final String ISFIRSTLAUCH = "isFirstLauch";
    private static final String SAVEVERSIONCODE = "saveVersionCode";
    private static final String SAVESTATUS = "saveStatus";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nowVersionCode = getVersionCode();
        SharedPreferences sp  = getSharedPreferences(ISFIRSTLAUCH,MODE_PRIVATE);
        saveVersionCode = sp.getFloat(SAVEVERSIONCODE,0);

        if (nowVersionCode > saveVersionCode) {
            //第一次安裝啓動
            SharedPreferences.Editor editor = sp.edit();
            editor.putFloat(SAVEVERSIONCODE, nowVersionCode);
            editor.putBoolean(SAVESTATUS, true);
            editor.commit();
            //初始化數據
           
        }
        startActivity(new Intent(this,MainActivity.class));

    }

    /**
     * 獲取應用版本號
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    private float getVersionCode(){
        float versionCode = 0;
        try {
            versionCode = getPackageManager().getPackageInfo(getPackageName(),0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章