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

后端 未结 4 1220
盖世英雄少女心
盖世英雄少女心 2020-12-14 04:14

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 prefer

相关标签:
4条回答
  • 2020-12-14 04:27

    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 讨论(0)
  • 2020-12-14 04:37

    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"); 
    }
    
    0 讨论(0)
  • 2020-12-14 04:41

    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.

    0 讨论(0)
  • 2020-12-14 04:52

    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.

    0 讨论(0)
提交回复
热议问题