3

I want to run a service every time I open my App after the App is updated or installed for the first time. How do I check if App is opened for the first time after launch?

I tried this but code in onCreate method but I'm getting isFirst= true on updating my code, however it should be blank/null so that I can run my service. Where am I wrong?

  @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 isFirst = sPrefs.getString(Utility.KEY_IS_FIRST, "");
    if(isFirst == null || isFirst.equalsIgnoreCase("")){
        //run service and set isFirst = true

        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(Activity_Home.this);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(Utility.KEY_IS_FIRST, "true");

        edit.commit();
}
}
  • what had you tried so far? .... obviously `boolean first = SP.getBoolean("some_prefix" + getCurrentAppVersionSomehow(), true); if(first) { doTheStuff(); changeSPValueToFalse(); }` would do the thing – Selvin Jan 13 '16 at 11:58
  • check [this](http://stackoverflow.com/questions/6662163/retrieve-package-name-of-applications-whom-are-currently-running-in-android) – Iamat8 Jan 13 '16 at 11:59
  • also check this http://stackoverflow.com/questions/16098042/how-to-get-running-application-package-name-using-broadcast-receiver – Iamat8 Jan 13 '16 at 11:59
  • Possible duplicate of [Check if application is on its first run](http://stackoverflow.com/questions/7217578/check-if-application-is-on-its-first-run) – Ravi Jan 13 '16 at 12:00
  • @Selvin Code added.. – beginner_android Jan 14 '16 at 05:33

3 Answers3

7

You could save the version code to your shared preferences and compare it to the version code of the currently running app. If your version code is higher than the saved then the user has updated, run your service and update the shared preferences.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int currentVersionCode = BuildConfig.VERSION_CODE;

if (prefs.getInt("LASTVERSION", 0) < currentVersionCode) {
    // Run service here
    prefs.edit().putInt("LASTVERSION", currentVersionCode).apply();
}
hibob
  • 746
  • 7
  • 17
  • In newer Android Studio projects it is easier to simply use `BuildConfig.VERSION_CODE` to get the current version code - Never mind, you just made the edit! – Ed Holloway-George Jan 13 '16 at 12:03
  • what if I have version 2.0 and i installed it first time. I means I am not install app till version get 2. In that case What happen? –  Jan 13 '16 at 12:14
  • On first run you won't have a last version code in your shared preferences so it will default to 0. Since that default is less than 2 it will run. – hibob Jan 13 '16 at 12:28
  • @hibob versionCode is not changing in my App every time on updating. Is there any other solution? – beginner_android Jan 14 '16 at 05:34
  • @beginner_android are you increasing the version code number in your build.gradle? This is something that would be required for each new release to the store so that's what we use to check if the user has upgraded. – hibob Jan 14 '16 at 08:24
  • The issue with the code you've edited into your original question is that after the first run the string in your shared preferences will always be set so your service will never run again. Save the version code to preferences instead and compare it to the current version each time instead (like in my code example) – hibob Jan 14 '16 at 08:55
1

Use SharedPareference, but use it to save your apps version number. Whenever it differs, run your service.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
0

You can use a boolean variable and store it in shared preferences. when you open your app first time set it to true and next time check for the value of that variable

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84