How to retain the global variebles when application is upgraded to new version in android?

谁说胖子不能爱 提交于 2020-01-11 12:07:11

问题


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 agreement page or email registration window. I am controlling it using global variables but if user upgrade application to newer version, global variables get reset, so user get the agreement page and registration window again. So how can I do this? Thanks for help in advance.


回答1:


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.




回答2:


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);
}
}



回答3:


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\&apos;s New" >
    </Preference>
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="displayChangelog"
        android:summary="@string/settings_changelog"
        android:title="Hide The Changelog" >
    </CheckBoxPreference>
</PreferenceCategory>


来源:https://stackoverflow.com/questions/13263855/how-to-retain-the-global-variebles-when-application-is-upgraded-to-new-version-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!