2015-10-21 46 views
0

我需要在用戶第一次安裝並運行應用程序時發出問候消息。 第一次運行應用程序的簡單方法是什麼?如何理解Android應用程序中的首次安裝?

+1

檢查SharedPreference布爾標誌。如果沒有設置,請顯示問候語,然後進行設置。 –

+0

謝謝Ken。我認爲你的設置比你說的更好。它可以默認爲空,並在首次安裝後設置每次檢查 – ulas

回答

1

使用SharedPreferences來存儲是否需要顯示消息的標誌。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
boolean showMsg = prefs.getBoolean("show_msg", true); 
if (showMsg) { 
    // show the message 
    // then save the flag 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putBoolean("show_msg", false); 
    editor.commit(); 
} 

把它放在onCreate()或其他類似的。

下次運行該代碼段時,該值將爲false,因此消息將不會顯示。

相關問題