Android-How can I know if it is the first time the

2019-03-31 04:53发布

问题:

This question already has an answer here:

  • Determine if android app is the first time used 13 answers

How can I know if it is the first time the application launched?

If you are answering please add a full code because I have read some answers and I didn't understand them.

Thanks.

回答1:

Use sharedPreferences for the persistent data storage.when the application first launched just save a boolean value in the shared Preferences.Then check each time.

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("isLauncedTime",true);
prefEditor.commit();


回答2:

Every app gets a way to store preferences or options, so you can have one for whether or not the app has previously run

SharedPreferences runCheck = PreferenceManager.getSharedPreferences("hasRunBefore", 0); //load the preferences
Boolean hasRun = runCheck.getBoolean("hasRun", false); //see if it's run before, default no
if (!hasRun) {
    SharedPreferences settings = getSharedPreferences("hasRunBefore", 0);
    SharedPreferences.Editor edit = settings.edit();
    edit.putBoolean("hasRun", true); //set to has run
    edit.commit(); //apply
    //code for if this is the first time the app has run
}
else {
    //code if the app HAS run before
}


回答3:

You might want to refer to http://developer.android.com/guide/topics/data/data-storage.html#pref.

You can load a boolean when you execute your application for the first time to the shared preferences, and then check if it true in following runs, so you know the program has already been run once.



回答4:

I don't know if this is the best solution, but...

I tried storing a simple boolean in the Android's file system and when starting the android app, checking if that boolean exists and what it's value is.

Again, not sure if this is the proper way to do it, it's just my own way of getting around this.