Android-How can I know if it is the first time the application launched? [duplicate]

孤者浪人 提交于 2020-01-01 06:10:20

问题


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.



来源:https://stackoverflow.com/questions/8617662/android-how-can-i-know-if-it-is-the-first-time-the-application-launched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!