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

2019-03-31 03:54发布

This question already has an answer here:

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.

4条回答
家丑人穷心不美
2楼-- · 2019-03-31 04:36

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.

查看更多
Juvenile、少年°
3楼-- · 2019-03-31 04:39

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();
查看更多
Viruses.
4楼-- · 2019-03-31 04:42

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.

查看更多
闹够了就滚
5楼-- · 2019-03-31 04:53

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
}
查看更多
登录 后发表回答