In my android application ,user has to register by agreeing the terms and condition giving their email id. If user upgrade the application to next version, I should not get the
I use this piece of code to display the ChangeLog in my app to detect if the version has changed and display the ChangeLog accordingly. The same logic can be used for displaying or not displaying the Agreement, as the case may be.
These are my Global Declarations:
// FOR STORING VERSION CODE IN SHAREDPREFERENCES
private static final String PRIVATE_PREF = "my_app_name";
private static final String VERSION_KEY = "version_number";
try {
// GET THE PREFERENCE SET FOR THE CHANGELOG
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final boolean showChanges = prefs.getBoolean("displayChangelog", false);
PackageInfo pkgInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
// SET THE CURRENT VERSION IN SHAREDPREFERENCES
SharedPreferences sharedPrefs = getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE);
// SAVED VERSION CODE
int savedVersionCode = sharedPrefs.getInt(VERSION_KEY, 0);String.valueOf(savedVersionCode));
// GET NEW VERSION CODE
int currentVersionCode = pkgInfo.versionCode;String.valueOf(currentVersionCode));
Editor editor = sharedPrefs.edit();
editor.putInt(VERSION_KEY, currentVersionCode);
editor.commit();
if (showChanges == false) {
/***** DISPLAY THE CHANGELOG *****/
// DISPLAY DIALOG WHEN THE APPLICATION IS INSTALLED FOR THE FIRST TIME
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("displayChangelog", true);
editor.commit();
} else if (showChanges == true && currentVersionCode > savedVersionCode) {
/***** DISPLAY THE CHANGELOG AFTER AN UPGRADE *****/
// DISPLAY DIALOG CHANGELOG AFTER AN UPGRADE
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("displayChangelog", true);
editor.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
EDIT: Alongwith this code, I also use a Preferences
XML file to provide the use the ability to see the ChangeLog again and also store the state of the user preference to display the ChangeLog at next run. The XML for that:
<PreferenceCategory
android:key="changeLogOptions"
android:title="Changelog Options" >
<Preference
android:key="changeLog"
android:summary="See what has changed in the current version"
android:title="What\'s New" >
</Preference>
<CheckBoxPreference
android:defaultValue="false"
android:key="displayChangelog"
android:summary="@string/settings_changelog"
android:title="Hide The Changelog" >
</CheckBoxPreference>
</PreferenceCategory>
You Have two ways to storing your data @ local media.
Shared Preference
SQLite Database
When i should use Shared Preference ?
If your number of storing variable are not more and it should be primitive values then you can use Shared Preference
.
See This for sample code .
Or
When i should use SQLite Database ?
If you have to store number of variable and which are not primitive and also you need that data for long time then you should use SQLite Database
.
See This for sample code .
Now its Depends on you as per your requirement.
You could use SharedPreferences
class VersionInfo {
private VersionInfo() {}
private static final String PREF_VERCODE = "VERCODE";
public static final VersionInfo INSTANCE = new VersionInfo();
void setVersionCode(Context ctx, int ver) {
PreferenceManager.getDefaultSharedPreferences(ctx).edit().putInt(PREF_VERCODE, ver).commit();
}
int getVersionInfo(Context ctx) {
PreferenceManager.getDefaultSharedPreferences(ctx).getInt(PREF_VERCODE, 1);
}
}