开发者

Android: How to reset FirstRun SharedPreferences when my app is updated?

开发者 https://www.devze.com 2023-02-05 03:05 出处:网络
My app copies files over from res/raw to the sdcard on first run. I want it to update those files on every subsequent app update. How can i have it reset the firstrun开发者_如何学编程 preference to tr

My app copies files over from res/raw to the sdcard on first run. I want it to update those files on every subsequent app update. How can i have it reset the firstrun开发者_如何学编程 preference to true on every app update?

Here is the relevant code:

/**
     * get if this is the first run
     *
     * @return returns true, if this is the first run
     */
        public boolean getFirstRun() {
        return mPrefs.getBoolean("firstRun", true);
     }

     /**
     * store the first run
     */
     public void setRunned() {
        SharedPreferences.Editor edit = mPrefs.edit();
        edit.putBoolean("firstRun", false);
        edit.commit();
     }

     SharedPreferences mPrefs;

     /**
     * setting up preferences storage
     */
     public void firstRunPreferences() {
        Context mContext = this.getApplicationContext();
        mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences
     }

     public void setStatus(String statustext) {
         SharedPreferences.Editor edit = mPrefs.edit();
         edit.putString("status", statustext);
         edit.commit();
      }

}


In my app, I save in my shared preferences the version code of the app. At every startup, I check to see if the saved version code is lower than my current version code. If it is, I show a "what's new" dialog.

Give this code a whirl - I use it in my main activity's onCreate:

    PackageInfo pInfo;
    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
        if ( prefs.getLong( "lastRunVersionCode", 0) < pInfo.versionCode ) {
            // TODO: Handle your first-run situation here

            Editor editor = prefs.edit();
            editor.putLong("lastRunVersionCode", pInfo.versionCode);
            editor.commit();
        }
    } catch (NameNotFoundException e) {
        // TODO Something pretty serious went wrong if you got here...
        e.printStackTrace();
    }

prefs is a private SharedPreferences object. This works if it's truly the first run, and for upgrades. At the end of the first-run code, the editor.putLong updates your shared preferences with the current version code of your app so the next run doesn't trigger your first-run code.

This also benefits from the fact that your version code must increase for the app to be seen as an upgrade by the market, so you don't need to remember to change a separate value to detect the first-run.


You could mimic what's done on the database side, with version numbers. Instead of having just a firstRun variable, have a couple with firstRun and versionNumber, and put a static version number field in your app, that you increment at each release. This way, you'll be able to check if the app has been updated, and do your operation on each update.


I'm create class for this; download in https://gist.github.com/2509913

Example Use:

long versionInstalled = App.getVersionInstalled(this);
long current_v = App.getVersion(this);
if( versionInstalled  != current_v ){
    Log.w("TAG", "Veresion not valid"); 
}


Run this in the MainActivity's OnCreate

public void onUpdateFirstRun () {
    SharedPreferences prefs  = PreferenceManager.getDefaultSharedPreferences(this);
    PackageInfo pInfo;

    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);

        Log.d("VersionCode", pInfo.versionCode + " ");
        if (prefs.getLong(LAST_RUN_VERSION_CODE_KEY, 0) < pInfo.versionCode) {

            if (!isInitializedInSP(KEY)) {

                editor.putString(KEY, "");
            }

            //Finalize and Save
            editor.putLong(LAST_RUN_VERSION_CODE_KEY, pInfo.versionCode);
            editor.apply();

        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

Use method to check if you had already initialized it in previous version

public static boolean isInitializedInSP (String key) {
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContex());
    Object o = mPrefs.getAll().get(key);

    if (o != null) {
        return true;
    }
    else  {
        return false;
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号