Monday, April 13, 2015

Android Shared Preferences

Saving values 

        Context ctx = AppUtils.getAppContext();
        if(ctx != null && prefKey != null && prefValue != null)
        {
            Log.v(LOG_TAG,"context value exists: "+ctx);
            this.sensorPreferenceStorage = ctx.getSharedPreferences(PREFERNCE_STORE,  AppUtils.getAppContext().MODE_PRIVATE);
            Log.v(LOG_TAG,"preference storage is :"+this.sensorPreferenceStorage);
            SharedPreferences.Editor editor = this.sensorPreferenceStorage.edit();
            editor.putString(prefKey,prefValue);
            editor.commit();
        }

Reading value 

        Context ctx = AppUtils.getAppContext();
        Log.v(LOG_TAG,"getting the Sensor preferece value for key:"+preferenceKey);
        if(ctx != null && preferenceKey != null)
        {
            this.sensorPreferenceStorage = ctx.getSharedPreferences(PREFERNCE_STORE,  AppUtils.getAppContext().MODE_PRIVATE);
            String strVal = this.sensorPreferenceStorage.getString(preferenceKey, null);
            Log.v(LOG_TAG,"value retrieved is:"+strVal);
            return strVal;
        }

References: 

No comments:

Post a Comment