Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App. How to detect in code if app is new install or update?

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?

like image 678
Alex Avatar asked Nov 01 '22 08:11

Alex


2 Answers

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".

like image 71
laalto Avatar answered Nov 09 '22 09:11

laalto


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;
}
like image 40
Yauraw Gadav Avatar answered Nov 09 '22 09:11

Yauraw Gadav