5

Besides having a custom solution by using SharedPreferences, is there a way I can detect if my running app is an update for a previously installed version vs a new installation?

Is there any singular value in the API I can query for that?

Alex
  • 836
  • 9
  • 19
  • There are ways to detect it but there are also specialized solutions for new install/upgrade. What is the problem you're trying to solve with this information? – laalto Dec 27 '13 at 21:28
  • You need to save data in your app , see [How to save data in an android app][1] [1]: http://stackoverflow.com/questions/10962344/how-to-save-data-in-an-android-app – Mlook Dec 27 '13 at 21:29
  • The problem is that I changed the formula for calculating the scores in my game. I need to get the current scores I have in SQLite and adjust it along with an MD5 value before posting to the server. Is not that easy to check if the score value I already have, used the previous or the latest formula. – Alex Dec 27 '13 at 22:20
  • Did you get any solution for this? I'm still looking for the solution. The sharedpreferences solution is a bit erratic. Sometimes it doesn't detect. Please help – huskygrad Jan 18 '17 at 10:31
  • sorry, I ended up adding custom code to solve my problem – Alex Jan 20 '17 at 23:46

2 Answers2

1

You can know by the versionCode!

You should go with versionCode to detect if the App is new install or update. Save the value of versionCode for the very first time user installs the application, when there will be a new update the new versionCode's value would be more than the previous versionCode, because version code will always increase.

The VersionName is just a string which is presented to user for user readability.

public int getVersion() {
    int v = 0;
    try {
        v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
        
    }
    return v;
}
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
  • 1
    Thanks Gauraw, that's pretty much what I had in mind when I referred to a custom version using SharedPreferences. I would prefer to check an API flag instead if it exists at all. – Alex Dec 27 '13 at 21:56
  • You can also check this link http://stackoverflow.com/questions/15150320/how-to-detect-that-an-update-of-my-app-is-available-on-google-play – Yauraw Gadav Dec 27 '13 at 21:57
1

The problem is that I changed the formula for calculating the scores in my game. I need to get the current scores I have in SQLite and adjust it along with an MD5 value before posting to the server. Is not that easy to check if the score value I already have, used the previous or the latest formula.

For updating the data in SQLite database on upgrade using SQLiteOpenHelper, just bump up the database version number and write the required migration code in onUpgrade(). The Android framework will take care of calling onCreate() or onUpgrade() in case the database needs to be created or upgraded.

For communicating with a backend server, include some app version information in the messages. This can be e.g. the manifest versionCode as suggested in other answers. It's easier for you to update the code in your backend server, so you can work around quirks in specific app versions at the backend side. If the version information is missing, you can assume the app is "old".

laalto
  • 150,114
  • 66
  • 286
  • 303
  • thanks @laalto. I'll follow your suggestion of sending the version to the server. As for the sqlite part, the formula change is just one of a few other reasons making me find a way to distinct brand new install from upgrade. – Alex Dec 30 '13 at 21:46