Determine if Android app is being used for the first time

前端 未结 16 1251
攒了一身酷
攒了一身酷 2020-11-22 12:47

I am currently developing an android app. I need to do something when the app is launched for the first time, i.e. the code only runs on the first time the program is launch

16条回答
  •  悲&欢浪女
    2020-11-22 13:23

    I like to have an "update count" in my shared preferences. If it's not there (or default zero value) then this is my app's "first use".

    private static final int UPDATE_COUNT = 1;    // Increment this on major change
    ...
    if (sp.getInt("updateCount", 0) == 0) {
        // first use
    } else if (sp.getInt("updateCount", 0) < UPDATE_COUNT) {
        // Pop up dialog telling user about new features
    }
    ...
    sp.edit().putInt("updateCount", UPDATE_COUNT);
    

    So now, whenever there's an update to the app that users should know about, I increment UPDATE_COUNT

提交回复
热议问题