How can I detect if a user is using my application for the first time?
I would like to know if the user is using the application for the first time. I am using SharedPreferences
, but I am not sure if I have the right logic.
How can I set my isFirstLaunched
boolean to true when the user first launches, and then immediately set to false after work has been done?
protected void onStart() {
super.onStart();
if(isFirstLaunch()){
populateDefaultQ开发者_开发百科uotes();
//Save the preferences, isFirstLaunch will now be false
SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isFirstLaunch", false);
editor.commit();
}
setupUI();
checkOrientation();
restoreCache();
}
private void populateDefaultQuotes(){
System.out.println("!!!!!! FIRST TIMER !!!!!!");
}
private boolean isFirstLaunch() {
// Restore preferences
SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", false);
return isFirstLaunch;
}
Replace the false
argument in getBoolean()
by true
, then the logic will fit.
boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", true);
This will return true
if there is no such setting.
you can register a BroadcastReceiver to listen for Intent.ACTION_PACKAGE_FIRST_LAUNCH
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_FIRST_LAUNCH
精彩评论