To find out if your application is updated:
Save the current version code in the general settings, and then use the following function in the main action.
public static AppStart checkAppStart(Context context, SharedPreferences sharedPreferences) {
PackageInfo pInfo;
AppStart appStart = AppStart.NORMAL;
try {
pInfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
int lastVersionCode = sharedPreferences.getInt(
Constants.LAST_APP_VERSION, -1);
int currentVersionCode = pInfo.versionCode;
appStart = checkAppStart(currentVersionCode, lastVersionCode);
sharedPreferences.edit()
.putInt(Constants.LAST_APP_VERSION, currentVersionCode).commit();
} catch (PackageManager.NameNotFoundException e) {
Log.w(LOG_TAG,
"Unable to determine current app version from package manager. Defensively assuming normal app start.");
}
return appStart
}
private static AppStart checkAppStart(int currentVersionCode, int lastVersionCode) {
if (lastVersionCode == -1) {
return AppStart.FIRST_TIME;
} else if (lastVersionCode < currentVersionCode) {
return AppStart.FIRST_TIME_VERSION;
} else if (lastVersionCode > currentVersionCode) {
Log.w(LOG_TAG, "Current version code (" + currentVersionCode
+ ") is less then the one recognized on last startup ("
+ lastVersionCode
+ "). Defensively assuming normal app start.");
return AppStart.NORMAL;
} else {
return AppStart.NORMAL;
}
}
AppStart is just an enumeration:
public enum AppStart {
FIRST_TIME,
FIRST_TIME_VERSION,
NORMAL
}
Then clear the cache:
fooobar.com/questions/195225 / ...
source
share