Android: how to call method only on installation of App

前端 未结 4 2104
粉色の甜心
粉色の甜心 2021-01-05 23:21

I need to call a method (or start an activity, or something else) that will update a file containing data the app needs.

But I want it to be done only once, when the

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 23:41

    to do something only once in app you need something like this :

    boolean mboolean = false;
    
    SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
    mboolean = settings.getBoolean("FIRST_RUN", false);
    if (!mboolean) {
     // do the thing for the first time 
      settings = getSharedPreferences("PREFS_NAME", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("FIRST_RUN", true);
                        editor.commit();                    
    } else {
     // other time your app loads
    }
    

提交回复
热议问题