簡體   English   中英

如何首次設置android App?

[英]How to setup android App for the first time?

在我的程序中,如何首次設置SQLite數據庫? 程序的結構是什么?

我能想到的唯一方法是保持第一次布爾值:

如果(isFirstInstall)setup();

這似乎很不專業。 是否完成了此類設置的onFirstInstall()調用?

您可以將布爾值存儲到SharedPreferences中,並在應用程序啟動時檢查值(是否是第一次)。

檢查sharedPreferences的文檔

希望該主題對您有幫助

嘗試這個

private SharedPreferences mPreferences;

    boolean firstTime = mPreferences.getBoolean("firstTime", true);
    if (firstTime) { 
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean("firstTime", false);
        editor.commit();
        showMiddleActivity();
    }

您可以嘗試使用此代碼。

調用checkFirstLaunch(); 來自onCreate()。

private boolean checkFirstLaunch() {
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            //int currentVersion = info.versionCode;
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            int lastVersion = prefs.getInt(PreferencesActivity.KEY_HELP_VERSION_SHOWN, 0);
            if (lastVersion == 0) {
                isFirstLaunch = true;
            } else {
                isFirstLaunch = false;
            }

        } catch (PackageManager.NameNotFoundException e) {
            Log.w(TAG, e);
        }
        return false;
    }

您得到布爾結果。

if (isFirstLaunch) {
    //code    
}

無需弄清楚是不是第一次啟動,您可以檢查數據庫是否存在,並在數據庫丟失時創建它。

public void createDataBase() throws IOException {
    if ( checkIfDataBaseExist() ) {
        // db exists, do nothing
    } else {
        // By calling this method and empty database will be created into
        // the default system path of your application
        this.getReadableDatabase();
        try {
            // Do you db copying here
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkIfDataBaseExist() {
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM